How to add model object to a third party Silverlight user control?<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

How to add model object to a third party Silverlight user control?

Posted on: September 2, 2011

It can happen that we do not have the control of the source code of a user control and we still want to add an object into it. This scenario happened to me recently when I had to add a collection of object and know when they are changed.

This is where dependency property come at the rescue. I won't explain in this post how dependency property works but show you how to use it with a collection and an event that will be trigged if this collection change.

First, you may wonder why having this collection of object in the user control is wanted. You are right that this collection should be in the view model of the application, and it is. In fact, this property will be binded to the view model and every time an object is added or removed from the collection in the view model, this one will notify with the binding the depencendy property that will raise our event to redraw the user control.

First, this require that you create a new user control that will contain the user control that does not have the property desired.

1<UserControl x:Class="MyNameSpace.MyUserControl" xmlns="" xmlns:x="" xmlns:d="" xmlns:mc="" xmlns:ThirdPartyNamespace="clr-namespace:ThirdPartyNamespace;assembly=ThirdPartyUserControlHere" d:DesignHeight="300" d:DesignWidth="400">
3<ThirdPartyNamespace:ThirdPartyUserControlHere /> </UserControl>

In the code behind of the created user control, we need to add the dependency property.

1public static readonly DependencyProperty MyCustomProperty = DependencyProperty.Register( "MyCustomProperty" , typeof (ObservableCollection<MyObject>) ,typeof (MyUserControl) ,new PropertyMetadata(null,OnMyObjectCollectionPropertyChanged) );
2``` The line 3 contains the first parameters that is the property name you want to access from your control. This property will be bound to the third party user control. The line 4 if the type of the property added. In our example, it's a collection of MyObject. The line 5 is the type of the user control we add this property. This should be the name of the new user control you just added. The line 6 is the callback when this property change.
4The next step is to add the collection of MyObject and the callback.
6 public ObservableCollection<MyObject > MyObjects { get { return (ObservableCollection<MyObject>) GetValue(MyCustomProperty); } set { SetValue(MyCustomProperty, value); } }
8private static void OnMyObjectCollectionPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) { ((MyUserControl) o).OnMyObjectCollectionPropertyChanged(e); }
10private void OnMyObjectCollectionPropertyChanged(DependencyPropertyChangedEventArgs e) { //Redraw code here! }

The Xaml of the custom user control that envelop the third party user control can now be bound to the view model.

1<x:MyCustomUserControl DataContext="{Binding Path=MyObjectViewModel, Mode=TwoWay}" Shapes="{Binding Path=MyRealObjectsCollection, Mode=TwoWay}" />