views:

28

answers:

1

Simplified example:

I have an object that models a user. Users have a first name and a last name. The UserViewModel has a dependency property for my Models.User object. In the declaration of the UserView's xaml, I want to bind a couple of TextBlocks to the first and last name properties.

What is the correct way to do this? Should I have readonly DependencyProperties for the name fields, and when the dependency property User is set, update them? Can the name fields be regular C# properties instead? Or, should I bind like this:

<TextBlock Text="{Binding User.FirstName}" />
+1  A: 

You typically will never use Dependency Properties in your ViewModel or Model classes. You'll want to have your ViewModel implement INotifyPropertyChanged instead.

If you do that, you can bind using the syntax above. (Though, if you want two-way binding to work appropriately, your "User" object will also need to implement INotifyPropertyChanged - otherwise, changes made in code to the user will not automatically reflect in the UI.)

Reed Copsey
I see that although I've worded it completely differently, this is somewhat covered here http://stackoverflow.com/questions/291518/inotifypropertychanged-vs-dependencyproperty-in-viewmodel.So what you are saying is that User should be a POCO, and when User is set, then the readonly FirstName and LastName (POCO) fields are also set, and PropertyChanged is signalled for each?
mos