wpf datagrid自动扩展第一组

ale*_*234 6 wpf expand datagrid

我有一个datagrid,其中itemsource绑定到一个GroupCollectionView.当我填充集合时,我希望第一个组自动查看为扩展,如何在wpf(codebehind或mvvm)中编码?

<DataGrid 
     ItemsSource="{Binding ResultColl}" 
     SelectedItem="{Binding Path=SelectedResultItem, Mode=TwoWay}"
     SelectionMode="Single" IsReadOnly="True" >
    <DataGrid.GroupStyle>
        <GroupStyle>
            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                <Expander>
                                    <Expander.Header>
                                        <StackPanel>
                                                <TextBox Text="{Binding Items[0].ID}" />
                                        </StackPanel>
                                    </Expander.Header>
                                    <ItemsPresenter />
                                </Expander>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GroupStyle.ContainerStyle>
        </GroupStyle>
    </DataGrid.GroupStyle>

    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Path=ID}"/>
        <DataGridTextColumn Binding="{Binding Path=Typ}"/>
        <DataGridTextColumn Binding="{Binding Path=Info}"/>
        <DataGridTextColumn Binding="{Binding Path=orderDate, StringFormat={}{0:dd-MM-yyyy}}"/>
    </DataGrid.Columns>
</DataGrid>
Run Code Online (Sandbox Code Playgroud)

在mvvm控制器中:

ListCollectionView tmp = new ListCollectionView(myList);
tmp.GroupDescriptions.Add(new PropertyGroupDescription("ID"));
ResultColl = tmp;
...
ListCollectionView _resultColl;
public ListCollectionView ResultColl
{
    get { return _resultColl; }
    set { _resultColl = value;

        RaisePropertyChanged("ResultColl");
        if (value != null && _resultColl.Count > 0)
            SelectedResultItem = _resultColl.GetItemAt(0) as ItemResult;
    }
}
Run Code Online (Sandbox Code Playgroud)

执行代码时,数据网格将被填充,第一个项目被选中,但组被折叠.

cha*_*n86 12

将IsExpanded属性添加到您的类并添加绑定到Expander:

<Expander IsExpanded="{Binding Items[0].IsExpanded}">
Run Code Online (Sandbox Code Playgroud)

将IsExpanded设置为first为true