用于WPF DataGrid的CollectionViewSource MVVM实现

时间:2012-09-26 10:11:48

标签: c# wpf mvvm datagrid collectionviewsource

我在MVVM中为WPF DataGrid实现了CollectionViewSource的小型演示。我非常感谢任何帮助来验证实现并评论这是否是使用CollectionViewSource的正确方法。

public class ViewModel : NotifyProperyChangedBase
{       
    private ObservableCollection<Movie> _movieList;
    public ObservableCollection<Movie> MovieList
    {
        get { return _movieList; }
        set
        {
            if (this.CheckPropertyChanged<ObservableCollection<Movie>>("MovieList", ref _movieList, ref value))
                this.DisplayNameChanged();
        }
    }

    private CollectionView _movieView;
    public CollectionView MovieView
    {
        get { return _movieView; }
        set
        {
            if (this.CheckPropertyChanged<CollectionView>("MovieView", ref _movieView, ref value))
                this.DisplayNameChanged();
        }
    }

    public ViewModel()
    {
          MovieView = GetMovieCollectionView(MovieList);
    }

    private void DisplayNameChanged()
    {
        this.FirePropertyChanged("DisplayName");
    }

    public void UpdateDataGrid(string uri)
    {            
        MovieView = GetMovieCollectionView(new ObservableCollection<Movie>(MovieList.Where(mov => uri.Contains(mov.ID.ToString())).ToList<Movie>()));
    }

    public CollectionView GetMovieCollectionView(ObservableCollection<Movie> movList)
    {
        return (CollectionView)CollectionViewSource.GetDefaultView(movList);
    }

XAML视图:

  <Window.Resources>
     <CollectionViewSource x:Key="MovieCollection" Source="{Binding MovieList}">
    </CollectionViewSource>
  </Window.Resources>
   <DataGrid Name="MyDG" 
             ItemsSource="{Binding MovieView}" 
             AutoGenerateColumns="True" />

守则背后:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.Resources.Add("TagVM", new TagViewModel());
        this.DataContext = this.Resources["TagVM"];
    }

    private void Hyperlink_Click(object sender, RoutedEventArgs e)
    {
        string uri = ((Hyperlink)sender).NavigateUri.ToString();
        ((ViewModel)this.DataContext).UpdateDataGrid(uri);
    }

Hyperlink_Click处理程序调用VM的UpdateDataGrid方法,传递逗号分隔的电影ID,然后使用扩展方法过滤MovieList集合。

3 个答案:

答案 0 :(得分:17)

您不应该创建observable集合和集合视图的新实例。将谓词分配给集合视图上的filter属性,并在需要过滤集合时调用Refresh。

public class ViewModel : NotifyProperyChangedBase
{       
    string uri;

    public ObservableCollection<Movie> MovieList { get; private set; }

    public CollectionView MovieView { get; private set; }

    public ViewModel(MoveList movieList)
    {
        MovieList = movieList;
        MovieView = GetMovieCollectionView(MovieList);
        MovieView.Filter = OnFilterMovie;
    }

    public void UpdateDataGrid(string uri)
    {     
        this.uri = uri;
        MovieView.Refresh();
    }

    bool OnFilterMovie(object item)
    {
        var movie = (Movie)item;
        return uri.Contains(movie.ID.ToString());
    }

    public CollectionView GetMovieCollectionView(ObservableCollection<Movie> movList)
    {
        return (CollectionView)CollectionViewSource.GetDefaultView(movList);
    }
}

答案 1 :(得分:2)

以下是实例化CollectionViewSource以在DataGrid中启用多重过滤的示例:http://www.codeproject.com/Articles/442498/Multi-filtered-WPF-DataGrid-with-MVVM

CollectionViewSource在XAML视图中实例化,但绑定到视图模型中实例化的对象集合。然后,视图模型使用CollectionViewSource过滤DataGrid中的数据。

关于实例化CollectionViewSource的正确方法是什么 - 这是值得商榷的。

答案 2 :(得分:0)

您可以直接执行此操作来跳过添加资源:DataContext = new TagViewModel();并正常执行绑定。但我强烈建议使用依赖注入。