3

i am using a XamDataGrid to display my data. Now I want to add different commands to each column.

Using the CellActivated event on the whole grid and then binding to ActiveCell wont work since the Viewmodel would have to know about the View and how to evaluate the Column from the object returned by ActiveCell.

I am looking for a way how to tell the XamDataGrid which command should be called.

I imagine something like this:

<igDP:Field Name="Dev"                  >
   <igDP:Field.Settings>
      <igDP:FieldSettings CellValuePresenterStyle="{StaticResource DevStyle}" ActivateCommand="{Binding DevCommand}/>
   </igDP:Field.Settings>
</igDP:Field>

I dont really care if the command has to be a property of my viewmodel or the dataitem.

How do i implement this?

Thank You

mtxviper
  • 101
  • 2
  • 8

1 Answers1

1

Attached Behavior and MVVM goes hand in hand.

Handle your event via attached behavior and supply Viewmodel.ICommand to it, which it would execute when the event is handled. You can then send the event args from the handled event across to the ViewModel.ICommand as command parameter.

Your attached property

 public static class MyBehaviors {

    public static readonly DependencyProperty CellActivatedCommandProperty
        = DependencyProperty.RegisterAttached(
            "CellActivatedCommand",
            typeof(ICommand),
            typeof(MyBehaviors),
            new PropertyMetadata(null, OnCellActivatedCommandChanged));

    public static ICommand CellActivatedCommand(DependencyObject o)
    {
        return (ICommand)o.GetValue(CellActivatedCommandProperty);
    }

    public static void SetCellActivatedCommand(
          DependencyObject o, ICommand value)
    {
        o.SetValue(CellActivatedCommandProperty, value);
    }

    private static void OnCellActivatedCommandChanged(
           DependencyObject d, 
           DependencyPropertyChangedEventArgs e)
    {
        var xamDataGrid = d as XamDataGrid;
        var command = e.NewValue as ICommand;
        if (xamDataGrid != null && command != null)
        {
           xamDataGrid.CellActivated +=
              (o, args) =>
                 {
                     command.Execute(args); 
                 };
        }
    }
}

Your XAML:

 <infragistics:XamDataGrid ...
        local:MyBehaviors.CellActivatedCommand="{Binding MyViewModelCommand}" />

Hope it helps.

WPF-it
  • 19,625
  • 8
  • 55
  • 71
  • Would this CellActivatedCommand be bound to just the one desired Column? You left out where you would set the CellActivatedCommand. – mtxviper Dec 28 '12 at 13:49