Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.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# 在可观察到的收集中应用where不';不要反思数据网格_C#_Wpf_Linq_Observablecollection - Fatal编程技术网

C# 在可观察到的收集中应用where不';不要反思数据网格

C# 在可观察到的收集中应用where不';不要反思数据网格,c#,wpf,linq,observablecollection,C#,Wpf,Linq,Observablecollection,我尝试“过滤”一个ObservableCollection并更新绑定的DataGrid ObservableCollection<Record> recordObservableCollection; recordObservableCollection = new ObservableCollection<Record>(GetData()); //GetData() returns IEnumerable<Record> dataGrid1.Item

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

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

dataGrid1.ItemsSource = recordObservableCollection;
observedcollection记录observedcollection;
recordObservableCollection=新的ObservableCollection(GetData())//GetData()返回IEnumerable
dataGrid1.ItemsSource=recordObservableCollection;
然后我尝试筛选此集合:

 recordObservableCollection = new ObservableCollection<Record>(recordObservableCollection.Where(filter));//filter is Func<Data.Record, bool>
recordObservableCollection=新的ObservableCollection(recordObservableCollection.Where(filter))//过滤器是Func
recordObservableCollection
更新良好


但是数据网格没有更新。

名为
recordObservableCollection
的字段或变量最初有一个值,过滤后有一个不同的值。因为您使用了两次
newobservedcollection(…)
,所以创建了两个单独的observedcollection实例

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

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

dataGrid1.ItemsSource = recordObservableCollection;

现在
ItemSource
将被设置为
recordobservecollection
的新值您名为
recordobservecollection
的字段或变量最初有一个值,过滤后有一个不同的值。因为您使用了两次
newobservedcollection(…)
,所以创建了两个单独的observedcollection实例

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

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

dataGrid1.ItemsSource = recordObservableCollection;

现在,
ItemSource
将设置为新值
recordobservedcollection
observedcollection
公开为公共属性


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

ObservableCollection作为公共属性公开


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

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 
  }
  • 将您在testViewModel中定义的属性绑定到网格

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

  • ObservaleCollection将获得更新,因为ObservaleCollection(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 
      }
    
  • 将您在testViewModel中定义的属性绑定到网格

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

  • 我不确定我是否理解,我应该创建公共ObservableCollection RcordObservableCollection属性,在setter{}中定义什么?使用wpf的最佳方法是在该属性中使用MVVM模式是数据的派生轮。好的,我正在更新我的帖子