我有MainWindow.xaml (视图)和MainWindowViewModel.cs (ViewModel)。在我的程序中,我有一个自定义类,用于在Worklist.Result中启动时加载数据。此时,我需要使用自定义过滤数据。如果我在xaml中创建CollectionViewSource,所有的显示都很完美,但是我不能将Filter事件绑定到CollectionViewSource。好吧,那我需要代码背后的CollectionView.但最后,DataGrid不显示数据(没有绑定错误,CollectionViewSource拥有所有记录)。为什么?示例1:(XAML创建的CollectionViewSource w/o过滤)一切正常!
MainWindow.xaml
...
<xdg:DataGridCollectionViewSource x:Key="DataItems"
Source="{Binding WorkList.Result}"
<xdg:DataGridCollectionViewSource.GroupDescriptions>
<xdg:DataGridGroupDescription PropertyName="Date"/>
</xdg:DataGridCollectionViewSource.GroupDescriptions>
</xdg:DataGridCollectionViewSource>-->
...
<xdg:DataGridControl VerticalAlignment="Stretch" Background="White" ItemsSource="{Binding Source={StaticResource DataItems}}" ... </xdg:DataGridControl>示例2:(CodeBehind-创建的CollectionViewSource w/o过滤)DataGrid!中没有记录):
MainWindow.xaml
<xdg:DataGridControl VerticalAlignment="Stretch" Background="White" ItemsSource="{Binding DataItems}" ... </xdg:DataGridControl>MainWindowViewModel.cs
...
public ICollectionView DataItems { get; private set; }
...
private void WorkList_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
DataItems = CollectionViewSource.GetDefaultView(WorkList.Result);
}然后,WorkList_PropertyChanged事件将在CollectionViewSource中引发所有数据,而在DataGrid中则不会。有人能帮忙解决这个问题吗?
发布于 2016-06-19 09:30:17
为了让WPF引擎知道DataItems已经更新了一个新值,您的DataItems需要通知PropertyChanged。
即使CollectionViewSource.GetDefaultView(WorkList.Result);的结果是ObservableCollection,视图也不知道它,因为没有DataItems更新的通知。
确保您的MainWindowViewModel,实现了INotifyPropertyChanged,并且您可以:
...
private ICollectionView _dataItems;
public ICollectionView DataItems {
get
{
return this._dataItems;
}
private set
{
this._dataItems = value;
this.OnPropertyChanged("DataItems"); // Update the method name to whatever you have
}
...https://stackoverflow.com/questions/37905339
复制相似问题