Intercepting DataContext Change in Windows and Windows Phone

Often when working with UserControls you need to wire/unwire logic when the DataContext of the control changes (eg it’s used on a page and the viewmodel which is bound to the page flows into the UserControl as the DataContext). Unfortunately prior to Windows 8.1 there wasn’t an easy way to do this. In Windows 8.1 you can do the following in order to hook up to the DataContextChanged event handler

public class TestControl : UserControl
{
    public TestControl()
    {
        DataContextChanged += TestControl_DataContextChanged;
    }

    void TestControl_DataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args)
    {
        // Do stuff with the new Data Context
    }
}

So, how would you do this prior to Windows 8.1 and for Windows Phone? Well, one option, and I’m not saying this is the only option, is to create a different dependency property and connect it using data binding. For example:

public class TestControl : UserControl

   public object CustomDataContext
    {
        get { return GetValue(CustomDataContextProperty); }
        set { SetValue(CustomDataContextProperty, value); }
    }

   public static readonly DependencyProperty CustomDataContextProperty =
        DependencyProperty.Register("CustomDataContext", typeof(object), typeof(TestControl), new PropertyMetadata(null,DataContextChanged));

    private static void DataContextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        // Do stuff with the new Data Context
    }
}

And when you use this control, you’ll need to data bind to the CustomDataContext property:

<local:TestControl CustomDataContext="{Binding}"/>

The “{Binding}” without a path simply means that the current DataContext for the control will be passed in. You’re then free to do whatever you want with the real DataContext as you can be assured it would have been set at this point.

Leave a comment