9

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}

14
  • What is the error that you're getting? Commented Jul 23, 2012 at 20:14
  • no errors. i just don't see the value (my default Foo string) in the textbox Commented Jul 23, 2012 at 20:19
  • What is your view...a window, a user control, a data template? How are you setting the datacontext of your view to your view model object? Commented Jul 23, 2012 at 20:30
  • One thing you're going to want to fix is that you need a "setter" for your Notes property if you want TwoWay binding. Commented Jul 23, 2012 at 20:37
  • 2
    Yeah, that's a drawback of using strings as the mechanism to notify a property has changed. I think some of the MVVM frameworks, e.g. PRISM, let you notify via the property itself (which in turn just uses some reflection I think). Glad you're all set. Commented Jul 23, 2012 at 21:05

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

10 Comments

i have not tried this particular binding before actually.. but it still doesn't seem to work..
@SonicSoul what exactly does work...Your notes inside a record aren't showing up in your textbox or when you type in the textbox, they aren't going to your record's notes...or both? Are there any errors in your Output window...like a binding error?
both do not work. i default my string to Foo. that doesn't show up. if i type in some other value in the textbox and press a button to look at the debugger.. the property still has "Foo" in it, so it was not affected, or showed.. not seeing any relevant errors in the Output
Does the output window show a System.Windows.Data binding error?
yes. "'Notes' property not found on 'object' ''MainViewModel' " Not sure why it is looking there.. that is the view model for the window.. where as i am in a user control with a different viewmodel set (and working, as my other properties bind fine)
|
1

Make sure that your CurrentRecord property is 1) being set and 2) notifying the UI layer that a change has occurred.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.