开发者

Binding doesn't work for navigation properties

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?


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 call Quote.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:

Public Sub New()
    AddHandler JobReference.AssociationChanged, _
        AddressOf Job_AssociationChanged
End Sub

Sub Job_AssociationChanged(sender As Object, e As CollectionChangeEventArgs)
    OnPropertyChanged("Job")
End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜