Click here to Skip to main content
15,305,517 members
Articles / Desktop Programming / WPF
Posted 22 Nov 2010

Tagged as


2 bookmarked

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

Rate me:
Please Sign up or sign in to vote.
3.50/5 (2 votes)
22 Nov 2010CPOL
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="..."
  ... />

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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Morten Nilsen
Norway Norway
No Biography provided

Comments and Discussions www bags-shoes com bags shoes,gucci book... Pin
bagsshoes2822-Nov-10 23:23
Memberbagsshoes2822-Nov-10 23:23 
GeneralWell, it doesn't all have to be new.. Just trying to help ne... Pin
Morten Nilsen22-Nov-10 21:25
MemberMorten Nilsen22-Nov-10 21:25 
GeneralReason for my vote of 2 Nothing new here. Just about every M... Pin
SledgeHammer0122-Nov-10 9:48
MemberSledgeHammer0122-Nov-10 9:48 
Reason for my vote of 2
Nothing new here. Just about every MVVM library out there has a view locator or uses MEF.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.