如何关闭(使用复选框)使用CollectionViewSource实现的WPF / MVVM DataGrid分组?

时间:2015-03-25 11:01:41

标签: wpf mvvm wpfdatagrid collectionviewsource

我正在实施datagrid分组。下面是我做的(省略了扩展器的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中实现它

2 个答案:

答案 0 :(得分:1)

我建议您将复选框绑定到viewmodel的bool属性,其中setter还会根据指定的值设置collectionview的分组状态。如下例所示:绑定到复选框状态的bool属性是GroupView,绑定到datagrid的集合是View。

C#ViewModel

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>

答案 1 :(得分:0)

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

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

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