绑定不适用于导航属性
我正在将 WPF 与实体框架绑定。
Window.DataContext 属性设置为 Quote。 此 Quote 有一个属性 Job,我必须触发它应该从服务器加载的 Quote.JobReference.Load
。
<ContentControl Content="{Binding Job}"
ContentTemplate="{StaticResource JobTemplateSummary}"/>
正如您在上面看到的,我试图将 ContentControl
绑定到窗口的 DataContext
,它是一个 StaticResource
Quote
班级。
我正在 Window.Load 事件处理程序中调用 Load。 我应该打电话到其他地方吗?
I am binding WPF with Entity-Framework.
The Window.DataContext property is set to a Quote.
This Quote has a property Job, that I have to trigger Quote.JobReference.Load
it should load from the server.
<ContentControl Content="{Binding Job}"
ContentTemplate="{StaticResource JobTemplateSummary}"/>
As you can see above, I am trying to bind a ContentControl
to the Window's DataContext
which is a StaticResource
Quote
class.
I am calling the Load in the Window.Load
even handler.
Should I've called somewhere else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是导航属性默认情况下不会调用
PropertyChanged
事件,因此当窗口绑定时(在 Page_Load 处理程序之前),JobReference 仍未加载,我们必须调用Quote。当作业属性更改时显式显示 OnPropertyChanged("Job")
,以便 WPF UI 知道刷新控件绑定。我将以下内容添加到
Quote
类中,这解决了问题:The problem was that Navigation Properties don't call
PropertyChanged
event by default so when the window is bound (which is before Page_Load handler) the JobReference was still not Loaded, we have to callQuote.OnPropertyChanged("Job")
explicitly when the job property changes, so the WPF UI knows to refresh the control binding.I added the following to the
Quote
class, and this solved the problem: