分配新值后将 ObservableCollection 绑定到 DataGrid

发布于 2024-10-17 22:22:49 字数 527 浏览 3 评论 0原文

这似乎是一个简单的问题,但我无法让它发挥作用。

我有一个具有以下属性的 UserControl:

public ObservableCollection<HL7Message> source {get; set;}

和以下 Binding:

<data:DataGrid x:Name="dgMessages" Grid.Row="2" AutoGenerateColumns="True" 
ItemsSource="{Binding source}" ></data:DataGrid>

来自父 UserControl 我在单击按钮时设置了值:

messagesGrid.source = src; //messagesGrid is the name of the UserCntrol above

我期望我的 DataGrid 被更新,但事实并非如此。你能指出我做错了什么吗?

It seems to be a simple problem, but I can't get it to work.

I have a UserControl with the following property:

public ObservableCollection<HL7Message> source {get; set;}

And the following Binding:

<data:DataGrid x:Name="dgMessages" Grid.Row="2" AutoGenerateColumns="True" 
ItemsSource="{Binding source}" ></data:DataGrid>

from a parent UserControl I set the value upon a button click:

messagesGrid.source = src; //messagesGrid is the name of the UserCntrol above

I'm expecting my DataGrid to be updated, but it's not. Can you please point at what I'm doing wrong?

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

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

发布评论

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

评论(2

电影里的梦 2024-10-24 22:22:49

自动属性遗憾的是不支持更改通知。因此,如果您设置 source 属性,DataGrid 将不会知道集合已更改。

一种可能性是为 messagesGrid.source 实现 INotifiyPropertyChanged code>-Property:

class YourUserControlClass: INotifyPropertyChanged

  public event PropertyChangedEventHandler PropertyChanged;

  protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) {    
    if (null != PropertyChanged) {        
           PropertyChanged(this,e);    
    }
  }

  ObservableCollection<HL7Message> m_source;

  public ObservableCollection<HL7Message> Source { g
        get{return m_source;}
        set{
            if(value != m_source){
                 m_source=value;
                 OnPropertyChanged("Source");
            } 
        }
  } 
  ....

请注意,我将Source的第一个字母写成大写,因为在.net中,属性通常是这样写的。您必须相应地更改绑定,因为绑定区分大小写。

<data:DataGrid x:Name="dgMessages" Grid.Row="2" AutoGenerateColumns="True"  ItemsSource="{Binding Source}" ></data:DataGrid> 

Auto-properties sadly do not support change-notification. Therefore the DataGrid will not know that the collection has been changed if you set the source-Property.

One possibility is to implement INotifiyPropertyChanged for the messagesGrid.source-Property:

class YourUserControlClass: INotifyPropertyChanged

  public event PropertyChangedEventHandler PropertyChanged;

  protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) {    
    if (null != PropertyChanged) {        
           PropertyChanged(this,e);    
    }
  }

  ObservableCollection<HL7Message> m_source;

  public ObservableCollection<HL7Message> Source { g
        get{return m_source;}
        set{
            if(value != m_source){
                 m_source=value;
                 OnPropertyChanged("Source");
            } 
        }
  } 
  ....

Please note, I have written the first letter of Source in UpperCase because in .net, properties are generally written so. You have to change your binding accordingly because Bindings are case sensitive.

<data:DataGrid x:Name="dgMessages" Grid.Row="2" AutoGenerateColumns="True"  ItemsSource="{Binding Source}" ></data:DataGrid> 
思念满溢 2024-10-24 22:22:49

问题是,当您单击按钮时 source 的引用发生变化时,没有任何信息可以告诉 UI 自行更新。您需要将 source 设置为依赖属性,或者实现 INotifyPropertyChanged,并在 source< 的 setter 中调用 PropertyChanged 事件。 /代码>。

private ObservableCollection<HL7Message> source;
public ObservableCollection<HL7Message> Source 
{ 
  get
  {
    return this.source;
  }

  set
  {
    this.source = value;
    this.NotifyPropertyChanged(() => this.Source);
  }
}

The problem is that when the reference for source changes on your button click, there is nothing to tell the UI to update itself. You will either need to make source a dependency property, or implement INotifyPropertyChanged, and invoke the PropertyChanged event in the setter for source.

private ObservableCollection<HL7Message> source;
public ObservableCollection<HL7Message> Source 
{ 
  get
  {
    return this.source;
  }

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