在ObservableCollection上应用的位置不会反映在datagrid上

时间:2011-06-23 06:46:36

标签: c# wpf linq observablecollection

我尝试“过滤”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没有更新。

3 个答案:

答案 0 :(得分:4)

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

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

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

dataGrid1.ItemsSource = recordObservableCollection;

现在ItemSource将设置为新值recordObservableCollection

答案 1 :(得分:0)

ObservableCollection<Record>公开为公共财产

使用ObservableCollection仅在您添加/删除列表中的项目时影响绑定。通过使用ObservableCollection,您不需要重置绑定到列表或DataGrid当您的集合更改时(不是集合中的项目更改)。但是,当您的数据对象属性发生更改时,它们没有任何效果。为此,您需要为DataObject实现INotifyPropertyChanged接口。

答案 2 :(得分:0)

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

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

假设您在test.xaml中有数据网格

- &GT;首先为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));
        }
    }

}

- &GT;在项目中添加一个名为testViewModel的类后,它将继承您的ViewModelBase类。

- &GT;现在在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中更新beacsue ...

现在回到test.xaml,你必须做两件事

  1. 在代码behing或xaml中设置test.xaml的dataContext (在InitializeComponent()之后的Code中,使viewmodel类成为一个intance并将其指定为DataContext,如下所示

      public test()
        {
        InitializeComponent();
    
        testViewModel  vm = new testViewModel();
        this.DataContext = vm;
        }
    
  2. 将您在testViewModel中定义的属性绑定到网格

     <Grid Name="MyGrid" DataContext="{Binding recordObservableCollection}">
      </Grid>