2017-03-24 9 views
0

ListViewグループ化が完了し、エクスパンダコントロールが追加されます。ListView/DataGridViewは、アイテムが選択されたときにグループ化を展開します。

<ListView.GroupStyle> 
    <GroupStyle> 
     <GroupStyle.ContainerStyle> 
      <Style TargetType="{x:Type GroupItem}"> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate> 
          <Expander IsExpanded="{Binding Mode=TwoWay, Path=IsSelected, RelativeSource={RelativeSource AncestorType=ListViewItem, Mode=FindAncestor}}"> 
           <Expander.Header> 
            <StackPanel Orientation="Horizontal"> 
             <TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="Gray" FontSize="16" VerticalAlignment="Bottom"/> 
             <TextBlock Text="{Binding ItemCount}" FontSize="22" Foreground="Green" FontWeight="Bold" FontStyle="Italic" Margin="10,0,0,0" VerticalAlignment="Bottom" /> 
             <TextBlock Text=" item(s)" FontSize="22" Foreground="Silver" FontStyle="Italic" VerticalAlignment="Bottom" /> 
            </StackPanel> 
           </Expander.Header> 
           <ItemsPresenter/> 
          </Expander> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </GroupStyle.ContainerStyle> 
    </GroupStyle> 
</ListView.GroupStyle> 

IsExpanded

<Expander IsExpanded="{Binding Mode=TwoWay, Path=IsSelected, RelativeSource={RelativeSource AncestorType=ListViewItem, Mode=FindAncestor}}"> 

のような、私は選択を変更する lv.SelectedIndexを設定し、それが動作しません!

わかりません。

+0

AのListViewItemはGroupItemの視覚的な祖先ではありません。これはGroupItemの子です。あなたは特定のグループを選択/拡張しようとしていますか、何をしようとしていますか? – mm8

+0

ListViewItemをGroupItemに変更しますが、機能しません。 検索機能を実装して、リストからキーワードを見つけて、それを自動選択したいとします。だからグループはリストが結果を見つけるときに拡大する必要がある。 –

答えて

0

XAMLデザイン:

<Window.Resources> 
    <Style x:Key="groupheaderstyle" TargetType="{x:Type GroupItem}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type GroupItem}"> 
        <Expander x:Name="exp" IsExpanded="True" Background="White" Foreground="Black"> 
         <Expander.Header> 
          <TextBlock Text="{Binding Gropname}" /> 
         </Expander.Header> 
         <ItemsPresenter /> 
        </Expander> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Window.Resources> 
<DataGrid x:Name="dgdata" HorizontalAlignment="Left" Height="269" VerticalAlignment="Top" Width="292"> 
    <DataGrid.GroupStyle> 
     <GroupStyle ContainerStyle="{StaticResource groupheaderstyle}"> 
      <GroupStyle.Panel> 
       <ItemsPanelTemplate> 
        <DataGridRowsPresenter /> 
       </ItemsPanelTemplate> 
      </GroupStyle.Panel> 
     </GroupStyle> 
    </DataGrid.GroupStyle> 
</DataGrid> 

XAML Design.cs

public class group 
    { 
     public string groupname { get; set; } 
     public string CLgroup { get; set; } 
     public string displayname { get; set; } 
    } 
    public Window4() 
    { 
     InitializeComponent(); 
     ObservableCollection<group> samdata = new ObservableCollection<group> 
     { 
      new group{groupname="Group1",CLgroup="xxx",displayname="demo1"}, 
      new group{groupname="Group1",CLgroup="yyy",displayname="demo2"}, 
      new group{groupname="Group1",CLgroup="yyy",displayname="demo2"}, 
     };   
     ListCollectionView collection = new ListCollectionView(samdata); 
     collection.GroupDescriptions.Add(new PropertyGroupDescription("groupname")); 
     dgdata.ItemsSource = collection; 
    } 
+0

あなたの答えをありがとうが、私のための助けはありません。 –

関連する問題