user290669
user290669

Reputation: 51

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

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

Upvotes: 5

Views: 16422

Answers (4)

Antoine Jaussoin
Antoine Jaussoin

Reputation: 5172

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.

Upvotes: 14

Damian
Damian

Reputation: 2789

For binding to the selected items I chose to create a behavior using System.Interactivity:

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Interactivity;
using Infragistics.Windows.DataPresenter;

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

        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) {
            SelectedDataItems = GetSelectedDataItems();
            //AssociatedObject.SetValue(SelectedDataItemsPropertyKey, SelectedDataItems);
        }

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

Some where in your view would have something like the following (I've ommitted the namespace mappings for brevity):

Now your problem with the command binding on a context menu thats something else... I'll revisit this

Upvotes: 2

Simon D.
Simon D.

Reputation: 4481

For a single item, infragistics was kind enough to add a bindable DependencyProperty called 'ActiveDataItem', which is "the" selected item, if any. It even works two-way, i.e. you can reset the selection from within your ViewModel.

Unfortunately, AFAIK there is no similar thing for multi-selection. You will have to implement this on your own, iterating over the selected records, check if they are datarecords, get the record and dataitem etc...

Upvotes: 6

Amsakanna
Amsakanna

Reputation: 12934

Try binding your DataGrid's SelectedItem to a property in your viewmodel.

You can then access this property in your OpenTradeHistory() method.

Upvotes: 2

Related Questions