65.9K
CodeProject is changing. Read more.
Home

How to hook up a ViewModel to a window using an attached property

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.50/5 (2 votes)

Nov 22, 2010

CPOL
viewsIcon

15013

Shows you how you can attach a viewmodel to your WPF window using attached properties, rather than setting the DataContext to a static resource or in code-behind

In my WPF-based projects, I have played around with many methods for setting the DataContext on my shell window, and the latest incarnation of this uses an attached DependencyProperty. Keep in mind, this is experimental code on my part.. First, we need to define our attached property:
public static class Application
{
    public static Type GetController(DependencyObject obj)
    {
        return (Type)obj.GetValue(ControllerProperty);
    }
    public static void SetController(DependencyObject obj, Type value)
    {
        obj.SetValue(ControllerProperty, value);
    }
    public static readonly DependencyProperty ControllerProperty =
            DependencyProperty.RegisterAttached("Controller", typeof(Type), typeof(Application), new UIPropertyMetadata(ControllerChanged));
    private static void ControllerChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if(e.NewValue != null)
        {
            ApplicationControllerBase.DefaultApplicationController = (ApplicationControllerBase)((Type)e.NewValue).GetConstructor(Type.EmptyTypes).Invoke(null);
        }
        ((FrameworkElement)d).DataContext = ApplicationControllerBase.DefaultApplicationController;
    }
}
The ApplicationControllerBase is my own base class for an application viewmodel, and the Default static property is useful for quickly navigating to the top tier from within a deep part. To set the DataContext in a window, I then add this to the XAML:
<Window x:Class="..."
  xmlns:app="..."
  xmlns:vm="..."
  vm:Application.Controller="app:..."
  ... />
Using this, we could pull in the application level data context anywhere in xaml, by using the vm:Application.Controller="app:..." tag on any element, it would get its DataContext set to the application view model.