Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.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# 动态分配时,ObservableCollection刷新不工作_C#_Wpf_Datagrid - Fatal编程技术网

C# 动态分配时,ObservableCollection刷新不工作

C# 动态分配时,ObservableCollection刷新不工作,c#,wpf,datagrid,C#,Wpf,Datagrid,如何通过使用段1获得自动刷新?您需要为ChildCommands属性实现INotifyPropertyChanged,例如: this.ChildCommands = m_context.ChildCommand.Local; 公共类YourClass:INotifyPropertyChanged { 私有可观测集合_childCommands; 公共事件属性更改事件处理程序属性更改; 公共可观测集合子命令 { 获取{return\u childCommands;} 设置 { _childCo

如何通过使用段1获得自动刷新?

您需要为
ChildCommands
属性实现
INotifyPropertyChanged
,例如:

this.ChildCommands = m_context.ChildCommand.Local;
公共类YourClass:INotifyPropertyChanged
{
私有可观测集合_childCommands;
公共事件属性更改事件处理程序属性更改;
公共可观测集合子命令
{
获取{return\u childCommands;}
设置
{
_childCommands=值;
OnPropertyChanged(“ChildCommand”);
}
}
受保护的void OnPropertyChanged(字符串名称)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(处理程序!=null)
{
处理程序(此,新PropertyChangedEventArgs(名称));
}
}
}
有关更多信息,请参见此:
我的解决方案是从NotificationObject类继承我的类

之后,只需使用

public class YourClass: INotifyPropertyChanged
  {
      private ObservableCollection<Model.ChildCommand> _childCommands;

      public event PropertyChangedEventHandler PropertyChanged;

      public ObservableCollection<Model.ChildCommand> ChildCommands
      {
          get { return _childCommands; }
          set
          {
              _childCommands= value;
              OnPropertyChanged("ChildCommands");
          }
      }

      protected void OnPropertyChanged(string name)
      {
          PropertyChangedEventHandler handler = PropertyChanged;
          if (handler != null)
          {
              handler(this, new PropertyChangedEventArgs(name));
          }
      }
  }

如果重新实例化集合,则不会更新集合的绑定。您有两个选择:

  • 在ChildCommands属性上实现INotifyPropertyChanged
  • 而不是做:

    this.ChildCommands=新的可观察集合(childs)

  • 改为这样做:

    this.ChildCommands = m_context.ChildCommand.Local;
    
    RaisePropertyChanged(() => this.ChildCommands); //this line notifies the UI that the underlying property has changed.
    

    段1没有自动更新的原因是您最终绑定到不同的ObservableCollection实例

    当m_context.ChildCommand.Local发生更改时,您的段2数据网格将收到通知,因为它已绑定到可观察的集合实例。但是,段1数据网格绑定到不同的可观察集合实例(当您说new ObservableCollection(childs)时,您自己创建了该实例)


    如果您确实希望两者都绑定到m_context.ChildCommand.Local observable collection,那么您应该这样实现它,而不是为段1创建不同的observable collection实例。

    您使用的是哪种UI?WPF或WinForms?ChildCommand=来自m_context.ChildCommand.Local中的child选择child////这样不行,你有办法解决我的问题吗?
    public class YourClass: INotifyPropertyChanged
      {
          private ObservableCollection<Model.ChildCommand> _childCommands;
    
          public event PropertyChangedEventHandler PropertyChanged;
    
          public ObservableCollection<Model.ChildCommand> ChildCommands
          {
              get { return _childCommands; }
              set
              {
                  _childCommands= value;
                  OnPropertyChanged("ChildCommands");
              }
          }
    
          protected void OnPropertyChanged(string name)
          {
              PropertyChangedEventHandler handler = PropertyChanged;
              if (handler != null)
              {
                  handler(this, new PropertyChangedEventArgs(name));
              }
          }
      }
    
    this.ChildCommands = m_context.ChildCommand.Local;
    
    RaisePropertyChanged(() => this.ChildCommands); //this line notifies the UI that the underlying property has changed.
    
    this.ChildCommands.Clear();
    
    foreach(var child in childs)
       this.ChildCommands.Add(child);