Message.Attach within DataTemplate

Topics: Getting Started, UI Architecture
Dec 31, 2012 at 11:53 PM

I've got the following (simplified) setup:

<DataGrid Name="Users" AutoGenerateColumns="False">
    <DataGridTemplateColumn Header="Actions">
              <!-- This is where the fun begins... -->
              <MenuItem Header="Suspend"
                    DataContext="{Binding Item, RelativeSource={RelativeSource FindAncestor, AncestorType=DataGridRow}}"
                    cal:Action.TargetWithoutContext="{Binding DataContext, ElementName=Users}"
                    cal:Message.Attach="SuspendUser($dataContext)" />
              <!-- Other MenuItems omitted -->

As you can see, the 'Action' column in my DataGrid contains a Menu, with a bunch of items on it. I'm binding these items to an action method on my ViewModel (not on the model present in the row!). In this example, I've got two methods on my ViewModel which look like this:

public Boolean CanSuspendUser(UserModel user)

public void SuspendUser(UserModel user)

The problems:

For each and every item, I'm having to specify the DataContext, the Target to invoke the actions on, and a manual message attachment. This makes for a lot of XAML.

Also, my guard methods are never called.

So, my question is...
Any ideas on how to do the above either with less 'fluff', or (ideally) in a better way entirely!


Thanks :)