Using MVVM, how to pass SelectedItems of a XamDataGrid as parameter to the Command raised by the ContextMenu?

user290669 picture user290669 · Jun 9, 2010 · Viewed 15.8k times · Source

I'm trying to pass the item on XamDataGrid on which I do a mouse right click to open a ContextMenu, which raises a Command in my ViewModel. Somehow the method that the Command calls is not reachable in debug mode.

This is the snipped from the view

<ig:XamDataGrid DataSource="{Binding DrdResults}" Height="700" Width="600">
  <ig:XamDataGrid.ContextMenu>
    <ContextMenu DataContext="{Binding RelativeSource={RelativeSource Mode=Self},
                               Path=PlacementTarget.DataContext}"
                 AllowDrop="True" Name="cmAudit">
      <MenuItem Header="View History" 
                Command="{Binding ViewTradeHistory}"
                CommandParameter="{Binding Path=SelectedItems}">
      </MenuItem>
    </ContextMenu>
  </ig:XamDataGrid.ContextMenu>
  <ig:XamDataGrid.FieldSettings>
    <ig:FieldSettings AllowFixing="NearOrFar"
                      AllowEdit="False" 
                      Width="auto" Height="auto"  />
  </ig:XamDataGrid.FieldSettings>
</ig:XamDataGrid>

My code in the corresponding ViewModel for this View is as follows.

public WPF.ICommand ViewTradeHistory
{
  get
  {
    if (_viewTradeHistory == null)
    {
      _viewTradeHistory = new DelegateCommand(
      (object SelectedItems) =>
      {
        this.OpenTradeHistory(SelectedItems); 
      });
    }
    return _viewTradeHistory;
  }
}

And lastly the actual method that gets called by the Command is as below

private void OpenTradeHistory(object records)
{
  DataPresenterBase.SelectedItemHolder auditRecords
    = (DataPresenterBase.SelectedItemHolder)records;
  // Do something with the auditRecords now.
}

I'm not sure what am I doing incorrectly here. Any help will be very much appreciated.

Thanks, Shravan

Answer

Antoine Jaussoin picture Antoine Jaussoin · Oct 18, 2012

I had that working by improving Damian answer (which was not quite working).

Here's my solution:

First the Behaviour:

public class DataGridExtender : Behavior<XamDataGrid>
{
    public readonly static DependencyProperty SelectedDataItemsProperty
        = DependencyProperty.Register(
            "SelectedDataItems",
            typeof(ICollection<object>),
            typeof(DataGridExtender),
            new PropertyMetadata());

    public ICollection<object> SelectedDataItems
    {
        get { return (ICollection<object>)GetValue(SelectedDataItemsProperty); }
        set { SetValue(SelectedDataItemsProperty, value); }
    }

    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.SelectedItemsChanged += AssociatedObjectOnSelectedItemsChanged;
        AssociatedObjectOnSelectedItemsChanged(AssociatedObject, null);
    }

    protected override void OnDetaching()
    {
        AssociatedObject.SelectedItemsChanged -= AssociatedObjectOnSelectedItemsChanged;
        base.OnDetaching();
    }

    private void AssociatedObjectOnSelectedItemsChanged(object sender, Infragistics.Windows.DataPresenter.Events.SelectedItemsChangedEventArgs e)
    {
        if (SelectedDataItems != null)
        {
            SelectedDataItems.Clear();
            foreach (var selectedDataItem in GetSelectedDataItems())
            {
                SelectedDataItems.Add(selectedDataItem);
            }
        }
    }

    private IEnumerable<object> GetSelectedDataItems()
    {
        var selectedItems = from rec in AssociatedObject.SelectedItems.Records.OfType<DataRecord>() select rec.DataItem;
        return selectedItems.ToList().AsReadOnly();
    }
}

And then its usage:

<igDP:XamDataGrid>
[...]

<i:Interaction.Behaviors>
    <Behaviours:DataGridExtender SelectedDataItems="{Binding SelectedDataItems, Mode=TwoWay}"></Behaviours:DataGridExtender>
</i:Interaction.Behaviors>

[...]

<igDP:XamDataGrid.FieldLayoutSettings>
    [...]
</igDP:XamDataGrid.FieldLayoutSettings>

<igDP:XamDataGrid.FieldLayouts>
    <igDP:FieldLayout>
        [...]
    </igDP:FieldLayout>
</igDP:XamDataGrid.FieldLayouts>

Of course you'll need to have a "SelectedDataItems" in your view model.

Edit: The SelectedDataItems property in the view model has to be instantited first as an empty collection, otherwise it won't work.