在 ObservableCollection 上应用 where 不会反映在数据网格上

发布于 2024-11-16 22:07:58 字数 602 浏览 1 评论 0原文

我尝试“过滤” ObservableCollection 并更新绑定的 DataGrid。

 ObservableCollection<Record> recordObservableCollection;
recordObservableCollection = new ObservableCollection<Record>(GetData()); //GetData() returns  IEnumerable<Record>

dataGrid1.ItemsSource = recordObservableCollection;

然后我尝试过滤这个集合:

 recordObservableCollection = new ObservableCollection<Record>(recordObservableCollection.Where(filter));//filter is Func<Data.Record, bool>

recordObservableCollection 更新得很好。

但 DataGrid 未更新。

I try to "filter" an ObservableCollection and update the bound DataGrid.

 ObservableCollection<Record> recordObservableCollection;
recordObservableCollection = new ObservableCollection<Record>(GetData()); //GetData() returns  IEnumerable<Record>

dataGrid1.ItemsSource = recordObservableCollection;

Then I try to filter this collection:

 recordObservableCollection = new ObservableCollection<Record>(recordObservableCollection.Where(filter));//filter is Func<Data.Record, bool>

recordObservableCollection is updated fine.

But the DataGrid is not updated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你如我软肋 2024-11-23 22:07:58

您的名为 recordObservableCollection 的字段或变量最初有一个值,过滤后有一个不同的值。因为您使用了 new ObservableCollection(...) 两次,所以您创建了两个单独的可观察集合实例。

问题在于 DataGrid 仍然引用第一个实例。即使您更改了 recordObservableCollection,也只会影响值。 DataGrid.ItemsSource 的值仍然是过滤之前的值

要解决此问题,您需要将新集合的值重新分配给 ItemSource 属性。只需重复您第一次执行的操作,但过滤之后:

dataGrid1.ItemsSource = recordObservableCollection;

现在 ItemSource 将设置为 recordObservableCollection 的新值。

Your field or variable called recordObservableCollection has one value initially and a different value after filtering. Because you used new ObservableCollection<Record>(...) twice you created two separate observable collection instances.

The problem is that the DataGrid is still referring to the first instance. Even though you have changed recordObservableCollection, that only affects its value. The value of DataGrid.ItemsSource is still what it was before the filtering.

To fix this problem, you need to re-assign the new collection's value to the ItemSource property. Simply repeat what you did the first time, but after the filtering:

dataGrid1.ItemsSource = recordObservableCollection;

and now ItemSource will be set to the new value of recordObservableCollection.

乜一 2024-11-23 22:07:58

ObservableCollection 将得到更新,因为 ObservableCollection (System.Collections.ObjectModel) 每次集合更改时都会抛出一个事件,但您必须再次将过滤器集合设置为 itemsource,否则它不会更新 UI...

执行此操作的最佳方法是使用您将在控件中将其绑定为项目源的公共属性,并且在该属性中,您将在 setter 中定义 NotifyPropertyChanged 。每次您使用此属性更改集合时,控件也会更新...

假设您的数据网格位于 test.xaml

-->首先,INotifyPropertyChanged 的​​所有工作在项目中添加一个抽象类,从 INotifyPropertyChanged 接口继承它并定义 OnPropertyChanged 方法

 public abstract class ViewModelBase : INotifyPropertyChanged
 {
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}

-->在您的项目中添加一个名为 testViewModel 的类后,它将继承您的 ViewModelBase 类..

-->现在在 testViewModel 中,您将为网格绑定创建一个属性,如下所示

  Private ObservableCollection<Record> _recordObservableCollection
  public  ObservableCollection<Record> recordObservableCollection
  {
  get
  {
       if(_recordObservableCollection == null)
        {
         _recordObservableCollection = new ObservableCollection<Record>(GetData());
         recordObservableCollection = new ObservableCollection<Record>(recordObservableCollection.Where(filter));
        }
       return _recordObservableCollection;
  }
 Set
  {

     _recordObservableCollection = Value;
      OnPropertyChanged("recordObservableCollection"); //Your property name 
  }

}

现在,如果您使用任何其他属性上的属性更新您的集合,方法或命令 UI 将更新,因为您已定义 OnPropertyChanged 的​​ setter 中...

现在返回对于这里的 test.xaml,您必须做两件事

  1. 在代码中或在 xaml 中设置 test.xaml 的 dataContext
    (在InitializeComponent()之后的代码中,创建一个viewmodel类的实例并将其分配为DataContext,如下所示

    <前><代码>公共测试()
    {
    初始化组件();

    testViewModel vm = new testViewModel();
    this.DataContext = vm;
    }

  2. 将您在testViewModel中定义的属性绑定到网格

     
        
    

ObservableCollection will get update because ObservableCollection (System.Collections.ObjectModel) throws an event every time the collection get changed but you have to set the filter collection as itemsource again other wise it wont update the UI...

The best way to do this use a public property that you'll bind in control as item source and in that property you will define NotifyPropertyChanged in setter . Every time you'll change the collection using this property the control will also be updated ...

Let Suppose you have your data grid in test.xaml

--> First fo all work for INotifyPropertyChanged add an abstract class in your project inherit it from INotifyPropertyChanged interface and define OnPropertyChanged method

 public abstract class ViewModelBase : INotifyPropertyChanged
 {
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}

--> After add a class in your project named it testViewModel which will inherit your ViewModelBase Class..

--> Now in testViewModel you'll make a property for your grid binding like this

  Private ObservableCollection<Record> _recordObservableCollection
  public  ObservableCollection<Record> recordObservableCollection
  {
  get
  {
       if(_recordObservableCollection == null)
        {
         _recordObservableCollection = new ObservableCollection<Record>(GetData());
         recordObservableCollection = new ObservableCollection<Record>(recordObservableCollection.Where(filter));
        }
       return _recordObservableCollection;
  }
 Set
  {

     _recordObservableCollection = Value;
      OnPropertyChanged("recordObservableCollection"); //Your property name 
  }

}

here now if u update your collection using property on any other property, method or command UI will be updated beacsue in setter you have defined OnPropertyChanged...

Now comes back to test.xaml here you have to do two things

  1. Set dataContext of test.xaml either in code behing or in in xaml
    (In Code behind just after InitializeComponent() make an intance of viewmodel class and assign it as DataContext like this

      public test()
        {
        InitializeComponent();
    
        testViewModel  vm = new testViewModel();
        this.DataContext = vm;
        }
    
  2. Bind property you defined in testViewModel to grid

     <Grid Name="MyGrid" DataContext="{Binding recordObservableCollection}">
      </Grid>  
    
醉殇 2024-11-23 22:07:58

ObservableCollection 公开为公共属性

,并且

仅当您从列表中添加/删除项目时,使用 ObservableCollection 才会影响绑定。通过使用 ObservableCollection,当集合发生变化(而不是集合内的项目发生变化)时,您无需重置与列表或 DataGrid 的绑定。但当您的数据对象属性发生更改时,它们不会产生任何影响。为此,您需要为 DataObject 实现 INotifyPropertyChanged 接口。

Expose the ObservableCollection<Record> as a public property

also

Using ObservableCollection only affect binding when you add/remove items from your list. By using ObservableCollection you do not need to reset binding to the list or DataGrid when your collection changed (not the item inside collection changed). But they do not have any effect when your data object properties changed. For that you need to implement INotifyPropertyChanged interface for your DataObject.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文