wpf datagrid 自动展开第一组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14317523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:01:00  来源:igfitidea点击:

wpf datagrid automatically expand first group

wpfdatagridexpand

提问by alexn234

I have a datagrid with the itemsource bound to a ListCollectionView with one group. When i fill the collection, i want the first group autmatically viewed as expanded, how to code that in wpf (codebehind or mvvm)?

我有一个数据网格,其中 itemsource 绑定到一个 ListCollectionView 和一组。当我填充集合时,我希望自动将第一组视为已展开,如何在 wpf(代码隐藏或 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>

In the mvvm controller:

在 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;
    }
}

When executing the code, the datagrid is filled the 1st item is selected but group is collapsed.

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

回答by chameleon86

Add IsExpanded property to your class and add binding to Expander:

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

<Expander IsExpanded="{Binding Items[0].IsExpanded}">

Set IsExpanded for first to true

首先将 IsExpanded 设置为 true

回答by Mr.Erlang

you can try add another bool property to your View Model defaulted to true but switching to false when first time used. And bind IsExpanded property of Expander to this with OneTime mode.

您可以尝试将另一个 bool 属性添加到默认为 true 但在第一次使用时切换为 false 的视图模型。并使用 OneTime 模式将 Expander 的 IsExpanded 属性绑定到此。

    public bool IsExpanded
    {
        get
        {
            if (_isExpanded)
            {
                _isExpanded = false;
                return true;
            }
            return false;
        }
    }

Xaml would be like that:

Xaml 应该是这样的:

<Expander IsExpanded="{Binding DataContext.IsExpanded, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Mode=OneTime}">