moswald
moswald

Reputation: 11677

How do I link (dependency) properties in my ViewModel?

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}" />

Upvotes: 0

Views: 457

Answers (1)

Reed Copsey
Reed Copsey

Reputation: 564441

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.)

Upvotes: 3

Related Questions