Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ListView/DataGridView分组&x27;选择项时,s展开_C#_Wpf_Data Binding - Fatal编程技术网

C# ListView/DataGridView分组&x27;选择项时,s展开

C# ListView/DataGridView分组&x27;选择项时,s展开,c#,wpf,data-binding,C#,Wpf,Data Binding,我完成了ListView分组并添加了一个expander控件 <ListView.GroupStyle> <GroupStyle> <GroupStyle.ContainerStyle> <Style TargetType="{x:Type GroupItem}"> <Setter Property="Template">

我完成了ListView分组并添加了一个expander控件

<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>
我将
lv.SelectedIndex
设置为更改选择,但它不起作用


我不知道。

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;
    }
公共类组
{
公共字符串组名{get;set;}
公共字符串CLgroup{get;set;}
公共字符串displayname{get;set;}
}
公共窗口4()
{
初始化组件();
ObservableCollection samdata=新的ObservableCollection
{
新组{groupname=“Group1”,CLgroup=“xxx”,displayname=“demo1”},
新组{groupname=“Group1”,CLgroup=“yyy”,displayname=“demo2”},
新组{groupname=“Group1”,CLgroup=“yyy”,displayname=“demo2”},
};          
ListCollectionView集合=新建ListCollectionView(samdata);
添加(新属性GroupDescription(“groupname”);
dgdata.ItemsSource=集合;
}

谢谢您的回答,但对我没有帮助。ListViewItem不是GroupItem的可视祖先。它是GroupItem的子项。您是否正在尝试选择/扩展特定的组,或者尝试执行什么操作?我将ListViewItem更改为GroupItem,但不起作用。我想实现一个搜索功能,从列表中查找关键字,然后自动选择。因此,当列表找到结果时,组需要展开。
 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;
    }