views:

29

answers:

1

Hi,

I have a custom user control, and it has a dependency property. That custom user control is kind of complicated so I decided to make a view model for it, but I haven't implemented it yet. I'm thinking of making the view model having some properties which are bound to the custom user control.

Here is my code sample,

-UserControl.xaml-

<StackPanel>
 <TextBlock Text={Binding Age} />
 <TextBlock Text={Binding Name} />
</StackPanel>

-UserControl.cs-

public Person Person
{
    get { return (Person)GetValue(PersonProperty); }
    set { SetValue(PersonProperty, value); }
}
public static readonly DependencyProperty PersonProperty = DependencyProperty.Register("Person", typeof(Person), typeof(SampleUserControl), new PropertyMetadata(null, propertyChangedCallback));

private static void propertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
 // I want to update the view model here
 // Something like the following
 (this.DataContext as MyViewModel).Person = Person; 
}

-MyViewModel-

public Person Person
{
  get { return _person; }
  set
  {
    _pserson = person;
    RaisePorpertyChanged("Age");
    RaisePorpertyChanged("Name");
  }
}

public int Age{ get; set; }
public string Name{ get; set; }

So, do you think it's a good practice? I mean updating a view model when a dependency property gets updated, and hopefully someone teaches me how to update the view model inside the PropertyChangedCallback :) BTW I'm using the MVVM Light toolkit.

Thanks in advance,

yokyo

A: 

According to this question, http://forums.silverlight.net/forums/p/133665/298671.aspx, I can't use a view model as the DataContext of a user control to bind some UI in a usual way.

Yoo Matsuo