【问题标题】:how to turn off (with a checkbox) WPF/MVVM DataGrid grouping that is implemented using CollectionViewSource?如何关闭(使用复选框)使用 CollectionViewSource 实现的 WPF/MVVM DataGrid 分组?
【发布时间】:2015-03-25 11:01:40
【问题描述】:

我有数据网格分组的工作实现。以下是我所做的(省略扩展器的 GroupStyle):

<CollectionViewSource x:Key="SelectedObjectsViewSource" Source="{Binding SelectedObjectItems}">
    <CollectionViewSource.GroupDescriptions>
       <PropertyGroupDescription PropertyName="TableId"/>
    </CollectionViewSource.GroupDescriptions>
</CollectionViewSource>


<DataGrid Name="SelectedObjectsGrid" 
     ItemsSource="{Binding Source={StaticResource SelectedObjectsViewSource}}"
     SelectionMode="Extended"
     CanUserAddRows="False"
     AutoGenerateColumns="False">

我想添加一个复选框,用户可以关闭/打开分组。但我不知道如何在 MVVM 中实现这一点

【问题讨论】:

    标签: wpf mvvm wpfdatagrid collectionviewsource


    【解决方案1】:

    我建议您将复选框绑定到视图模型的 bool 属性,其中它的设置器还设置集合视图的分组状态,遵循分配的值。如下例所示:绑定到复选框状态的 bool 属性是 GroupView,绑定到数据网格的集合是 View。

    C# 视图模型

    class ViewModel : INotifyPropertyChanged {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    
        private ICollectionView _View;
        public ICollectionView View
        {
            get
            {
                return _View;
            }
            set
            {
                _View = View;
                NotifyPropertyChanged("View");
            }
        }
    
        private bool _GroupView;
        public bool GroupView
        {
            get
            {
                return _GroupView;
            }
            set
            {
                if (value != _GroupView)
                {
                    // Clear Grouping status of the view
                    View.GroupDescriptions.Clear();
                    if (value)
                    {
                        // If true set Grouping status
                        View.GroupDescriptions.Add(new PropertyGroupDescription("TableId"));
                    }
                    _GroupView = value;
                    NotifyPropertyChanged("GroupView");
    
                    // Notify the UI that also the View changed in order to redraw the datagrid with or without grouping
                    NotifyPropertyChanged("View");
                }
            }
        }   
    }
    

    背后的 C# 代码

    public partial class MyWindow : Window
    {
        public MyWindow()
        {
            InitializeComponent();
            ViewModel myViewModel = new ViewModel();
            myViewModel.View = .....;
            DataContext = myViewModel;
    
        }
    }
    

    XAML

    <StackPanel>
        <CheckBox IsChecked="{Binding GroupView, Mode=TwoWay}"/>
        <DataGrid Name="SelectedObjectsGrid" 
                ItemsSource="{Binding View, Mode=TwoWay}"
                SelectionMode="Extended"
                CanUserAddRows="False"
                AutoGenerateColumns="False"/>
    </StackPanel>
    

    【讨论】:

    • 谢谢!从 xaml 移动 groupview 感觉很糟糕,但我猜这是唯一的方法。
    【解决方案2】:

    您可以在当前视图中创建另一个 DataGrid:

    <DataGrid Name="SelectedObjectsGridWithoutGrouping" Visibility="False" 
     ItemsSource="{Binding NewCollection}"
     SelectionMode="Extended"
     CanUserAddRows="False"
     AutoGenerateColumns="False">
    

    之后,您必须在 ViewModel 中创建 NewCollection(其中包含 SelectedObjectItems 集合中的项目,但没有分组),并且当用户更改复选框时,通过更改 Visibility SelectedObjectsGrid 或 SelectedObjectsGridWithoutGrouping 来显示或隐藏其中之一。

    【讨论】:

    • 谢谢!这可能有效,但我希望有另一种解决方案。
    猜你喜欢
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    • 2017-09-02
    • 2023-03-18
    • 2011-05-23
    • 2011-06-03
    • 2020-02-12
    • 1970-01-01
    相关资源
    最近更新 更多