views:

48

answers:

2

I'm trying to get my head around the whole MVVM thing and binding. I have a ViewModel class which has a property that is another class. I want to bind to a (string) property of that class to the text of a textblock.

I set the ViewModel as my data context for my window\page. And then do this:

<TextBlock Text="{Binding ElementName=myAddressClass, Path=StreetName}" />

But this does not work. The text is empty.

I can expose the StreetName directly as below and this works:

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

So am I doing something wrong in the first example. It seems simple enough ... am I just confuse about what an elementname is or should be set to?

thanks

+1  A: 

I think you probably are confused. If you want to bind to MyAddress.StreetName, just do this: Text="{Binding MyAddress.StreetName}" Make sure MyAddress is a property of your DataContext. ElementName is for binding to other controls.

Eric Mickelsen
+1  A: 

ElementName is used to reference a XAML element in the Logical Tree. Since what you're trying to bind to is not an element, ElementName isn't the correct approach. Dotted path notation is the simplest approach in this case:

{Binding Path=myAddressClass.StreetName}
hemp