编辑:我创建了一个新的 VS2010 WPF 应用程序,只有 3 个文件 MainWindow.xaml、MainWindow.xaml.cs 和 MainWindowViewModel.cs(如下所列)。如果有人觉得真的很有帮助,您可以在几秒钟内重现问题(复制/粘贴)。当您运行应用程序时,DataGrid 将显示错误的字符串“OldCollection”。如果将 ItemsSource 绑定更改为 MyCollection,它会显示正确的“NewCollection”。
完整描述: 起初我有一个 DataGrid,它的 ItemsSource 绑定到 MyCollection。我有/需要一个将新的 ObservableCollection<> 分配给 MyCollection 的方法 UpdateCollection。将 NotifyPropertyChange 添加到 MyCollection 后,UI 会更新。
接下来有必要引入一个 CollectionViewSource 来启用分组。将 UI 绑定到 MyCollectionView 后,对 UpdateCollection 的调用现在无效。调试器确认 MyCollectionView 始终包含初始 MyCollection。如何让我的 NewCollection 反映在视图中?我尝试过 View.Refresh()、Binding CollectionViewSource 和无数其他策略。
注意:主要是其他人关心对 Collection 项的更改,而不是在不调用 Refresh 的情况下更新视图(分组/排序)。我的问题是我正在为 CollectionViewSource 分配一个全新的集合,并且视图/用户界面永远不会改变。
// MainWindow.xaml
<Window x:Class="CollectionView.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid Name="grid" ItemsSource="{Binding MyCollectionView}" />
</Grid>
</Window>
//MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
namespace CollectionView
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowViewModel();
}
}
}
//MainWindowViewModel.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.ComponentModel;
namespace CollectionView
{
class MainWindowViewModel : INotifyPropertyChanged
{
public MainWindowViewModel()
{
MyCollection = new ObservableCollection<MyObject>() { new MyObject() { TestString = "OldCollection" } };
MyCollectionViewSource = new CollectionViewSource();
// Bind CollectionViewSource.Source to MyCollection
Binding MyBind = new Binding() { Source = MyCollection };
BindingOperations.SetBinding(MyCollectionViewSource, CollectionViewSource.SourceProperty, MyBind);
// The DataGrid is bound to this ICollectionView
MyCollectionView = MyCollectionViewSource.View;
// This assignment here to demonstrate that View/UI does not update to show "NewCollection"
MyCollection = new ObservableCollection<MyObject>() { new MyObject() { TestString = "NewCollection" } };
}
// Collection Property
// NotifyPropertyChanged added specifically to notify of MyCollection re-assignment
ObservableCollection<MyObject> _MyCollection;
public ObservableCollection<MyObject> MyCollection
{
get { return _MyCollection; }
set
{
if (value != _MyCollection)
{
_MyCollection = value;
NotifyPropertyChanged("MyCollection");
}
}
}
public CollectionViewSource MyCollectionViewSource { get; private set; }
public ICollectionView MyCollectionView { get; private set; }
// Method updates MyCollection itself (Called via ICommand from another ViewModel)
public void UpdateCollection(ObservableCollection<MyObject> NewCollection)
{
MyCollection = NewCollection;
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
class MyObject
{
public string TestString { get; set; }
}
}
谢谢,