XAML - Binding to complex types

Lets say you you have a User object with properties that return simple built-in types:
string Name, bool IsActive ...etc.

If you bind the controls parent to User, then with simple types you can do something like this:

<TextBlock Text="{Binding Name}"/>

well, if you add a complex type to the User, such as Contact of type ContactInfo that has its own set of properties - string Address, string Phone,  string Email,  ...etc. 

How do you bind then?

<TextBlock  Text="{Binding Contact.Email}"/>
won't work ( overriding ToString() on ContactInfo... or writing a Converter did not work for me)

What did work was:

settting the DataContext to the complex type property, then setting a binding path on XAML Control's Text, Content (...depending on the control that you are using) to the "inner" Property.

<TextBox DataContext="{Binding Contact}", Text="{Binding Path=Email}"/>

No comments:

Post a Comment