简体   繁体   English

WPF中数据网格的分组与非分组之间的切换

[英]Toggling between Grouping and non grouping of datagrid in WPF

I have currently populated a datagrid with certain amount of rows and grouped the rows based on department. 我目前已经用一定数量的行填充了一个数据网格,并根据部门对这些行进行了分组。 So ,all I can see in my output is a "Department-based Grouped Datagrid". 因此,我在输出中看到的只是一个“基于部门的分组数据网格”。

Is it possible to toggle between grouping and non grouping of datagrid rows ? 是否可以在数据网格行的分组和非分组之间切换?

For Example : If a user doesnt wants to see records based on groups, he'll click on radiobutton and datagrid will populate rows without grouping and vice-versa. 例如:如果用户不想查看基于组的记录,则单击单选按钮,然后datagrid将填充行而不进行分组,反之亦然。

Thanks in advance. 提前致谢。

Here is sample code inside DataGrid.GroupStyle : 这是DataGrid.GroupStyle内的示例代码:

            <DataGrid.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding Path=Name}"/>
                        </StackPanel>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <Expander Margin="15 0 0 0" IsExpanded="True">
                                        <Expander.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <TextBlock  Foreground="white" Text="{Binding Path=emp}" FontWeight="Bold" Margin="5 0 0 0"/>
                                                <TextBlock  Foreground="white" Text="{Binding Path=empCount}" Margin="10 0 3 0"/>
                                                <TextBlock  Foreground="white" Text="emps"/>
                                            </StackPanel>
                                        </Expander.Header>
                                        <ItemsPresenter />
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </DataGrid.GroupStyle>

Sorry if this is old, but I might as well answer. 对不起,如果这是旧的,但我还是会回答。

The edit you need to do are not in your GroupStyle but in the code of your ViewModel. 您需要进行的编辑不在GroupStyle中,而在ViewModel的代码中。 You probably bind your listview ItemSource to an ObservableCollection<>. 您可能将listview ItemSource绑定到ObservableCollection <>。 Internally A view of your collection is made, and this collectionView is the one that is actually bound to your ListView. 在内部,将对您的集合进行查看,并且该collectionView实际上是绑定到ListView的视图。 You can explicitly obtain that view in this way: 您可以通过以下方式显式获取该视图:

public ObservableCollection<T> HeldCollection{ get; set; }


        #region properties for the view
        CollectionView _HeldView;
        public CollectionView HeldView
        {
            get
            {
                if (_HeldView == null)
                {
                    _HeldView = (CollectionView)CollectionViewSource.GetDefaultView(HeldCollection);
                    //_HeldView.GroupDescriptions.Add(GroupDescription);
                }
                return _HeldView;
            }
        }

You can then edit the GroupDescription since it is a property of the CollectionView, for example in response to a button toggle. 然后,您可以编辑GroupDescription,因为它是CollectionView的属性,例如,响应于按钮切换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM