Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

wpf - Binding to ViewModel.SubClass.Property (sub-property)

let's say i have a section on my screen where "current record" is edited.. so my view model has a class with all currently edited properties such as:

class Record { 
    public string Notes { get { return "Foo"; } set { _notes = value; Notify("Notes"); }
}

and we add this class to the view model:

class AdjustsmentViewModel {
    public Record CurrentRecord { get { return new Record(); }}
}

How can i bind to Notes property of CurrentRecord in my view? I tried this:

<TextBox Text="{Binding CurrentRecord.Notes, Mode=TwoWay}" VerticalScrollBarVisibility="Auto" TextWrapping="WrapWithOverflow" AcceptsReturn="True"  />

This is not working however. I also tried setting DataContext of surrounding StackPanel:

<StackPanel DataContext="{Binding CurrentRecord}">

After that, i tried in my TextBox {Binding Notes} and {Binding Path=Notes}, but none of these seem to work.

Perhaps above really should work and i am messing something elsewhere?

Update

This is happening in a user control. This user control has a separate view model, from it's parent window.

this.DataContext = UnityUtil.Resolve<AdjustmentsViewModel>();

Also i am seeing a binding error: 'Notes' property not found on 'object' ''MainViewModel'

that view model is set on the main window.

to verify that i have the right ViewModel bound, i just added this property directly on the viewmodel:

public string Notes2 { get { return "Bar"; } } 

and corresponding textblock in the view:

<TextBlock Text="{Binding Path=Notes2}" />

this works as expected.

Great Success

Thanks to Ryan, i was able to find the problem. It was not in the property itself, but the way CurrentRecord was being set. In my setter, i make a call to INotifyPropertyChange handler, but that had the old name of the property in it. So the view was not getting a CurrentRecord notification, so i guess Notes notification was not enough ..

in conclusion, this notation is correct: {Binding Path=CurrentRecord.Notes}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The above should work, {Binding Path=CurrentRecord.Notes} is right. Can you check that your datacontext is set to your viewmodel?

Also check if your viewmodel implements INotifyPropertyChanged.

edit: I just created a sample project to recreate this. No need to implement INotifyPropertyChanged, it just works when the datacontext is set to the VM.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...