我在连接到视图的演示者中有一个对象。 在我的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.
更多推荐
发布评论