更新对象后刷新wpf数据绑定(Refreshing a wpf databinding after object has been updated)

编程入门 行业动态 更新时间:2024-10-28 12:29:47
更新对象后刷新wpf数据绑定(Refreshing a wpf databinding after object has been updated)

我在连接到视图的演示者中有一个对象。 在我的XAMTL里面,我有以下内容:

<Label Content="{Binding ElementName=PSV, Path=Presenter.Portfolio.Name}"/>

现在,当创建控件时,Portfolio为null,然后我运行另一个设置Portfolio的方法。 我已经实现了INotifyPropertyChanged,但到目前为止,我还没有能够触发绑定到绑定。

有人可以给我提示吗? 我可以绑定到属性的属性吗?

I have a object in a presenter connected to a view. Inside my XAMTL I have the following:

<Label Content="{Binding ElementName=PSV, Path=Presenter.Portfolio.Name}"/>

Now when the control is created, Portfolio is null, then i run another method which sets Portfolio. I've implemented INotifyPropertyChanged but so far, I've not been able to trigger to hookup to the binding.

Can someone give me tips? Can I bind to a property of a property ?

最满意答案

绑定始终适用于将Presenter设置为本地DataContext所需的DataContext。 例如,您可以在Window或UserControl的构造函数中执行此操作:

this.DataContext = new Presenter();

然后您的绑定将更改为:

<Label Content="{Binding ElementName=PSV, Path=Portfolio.Name}"/>

前一个路径的Presenter部分隐含在DataContext中。

这样,DataContext正在监视NotifyChanged事件,并且当Portfolio从null更改为具有值时,将正确更新视图。

在回答问题的最后部分时,绑定到属性的属性确实有效。

Binding always works with the DataContext you would need to set your Presenter into the local DataContext. For example you could do this in the constructor of your Window or UserControl:

this.DataContext = new Presenter();

Your binding would then change to:

<Label Content="{Binding ElementName=PSV, Path=Portfolio.Name}"/>

The Presenter part of your former path is implicit in the DataContext.

This way the DataContext is watching for the NotifyChanged events and will update the view properly when Portfolio changes from null to having a value.

In answer to the final part of your question, binding to a property of a property does work.

更多推荐

本文发布于:2023-07-15 05:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1110591.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   对象   数据   wpf   updated

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!