Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 绑定MVVM后CollectionViewSource代码的奇怪行为_C#_Wpf_Mvvm_Datagrid_Collectionviewsource - Fatal编程技术网

C# 绑定MVVM后CollectionViewSource代码的奇怪行为

C# 绑定MVVM后CollectionViewSource代码的奇怪行为,c#,wpf,mvvm,datagrid,collectionviewsource,C#,Wpf,Mvvm,Datagrid,Collectionviewsource,我有MainWindow.xaml(视图)和MainWindowViewModel.cs(视图模型)。 在我的程序中,我有一个自定义类,用于在Worklist.Result(observablecollection)中启动时异步加载数据。此时,我需要使用自定义筛选数据。如果我在xaml中创建CollectionViewSource,所有内容都会完美显示,但我无法将筛选器事件绑定到CollectionViewSource。好的,那么我需要CollectionView后面的代码。。。但最后DataG

我有MainWindow.xaml(视图)和MainWindowViewModel.cs(视图模型)。 在我的程序中,我有一个自定义类,用于在Worklist.Result(observablecollection)中启动时异步加载数据。此时,我需要使用自定义筛选数据。如果我在xaml中创建CollectionViewSource,所有内容都会完美显示,但我无法将筛选器事件绑定到CollectionViewSource。好的,那么我需要CollectionView后面的代码。。。但最后DataGrid不显示数据(没有绑定错误,CollectionViewSource拥有所有记录)。为什么? 示例1:(XAML创建的CollectionViewSource不带过滤)一切正常
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>
<xdg:DataGridControl VerticalAlignment="Stretch" Background="White" ItemsSource="{Binding DataItems}" ... </xdg:DataGridControl>

然后,WorkList_PropertyChanged事件引发CollectionViewSource中的所有数据,但不引发DataGrid中的所有数据。有人能帮忙解决这个问题吗

为了让WPF引擎知道数据项已更新为新值, 您的
数据项
需要通知
属性已更改

即使
CollectionViewSource.GetDefaultView(WorkList.result)的结果
,是一个ObservableCollection,视图不知道它,因为没有数据项已更新的通知

确保您的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
  }
...

您是否为属性数据项引发PropertyChanged?噢!我认为CollectionViewSource实现了自动RaisePropertyChanged(作为ObservableCollection)。麻烦的借口!当然有,但是您更改了DataItems属性,并且必须通知该更改。CVS不知道您将其分配给哪个属性,因此它本身无法通知您;o) 如果你想摆脱那种陈词滥调,那就用
...
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
  }
...