使用 MVVM,如何将 XamDataGrid 的 SelectedItems 作为参数传递给 ContextMenu 引发的命令?

发布于 2024-09-05 09:37:33 字数 1619 浏览 8 评论 0原文

我试图传递 XamDataGrid 上的项目,在该项目上单击鼠标右键以打开 ContextMenu,这会在我的 ViewModel 中引发一个命令。不知何故,在调试模式下无法访问命令调用的方法。

这是从视图中截取的内容,

<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>

该视图对应的 ViewModel 中的代码如下。

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

最后,命令调用的实际方法如下,

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

我不确定我在这里做错了什么。任何帮助将非常感激。

谢谢, 什拉万

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦开始←不甜 2024-09-12 09:37:33

我通过改进达米安的答案(这不太有效)来实现这一点。

这是我的解决方案:

首先是行为:

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();
    }
}

然后是它的用法:

<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>

当然,您的视图模型中需要有一个“SelectedDataItems”。

编辑:视图模型中的SelectedDataItems属性必须首先实例化为空集合,否则它将不起作用。

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.

不美如何 2024-09-12 09:37:33

对于单个项目,infragistics 足够友善地添加了一个名为“ActiveDataItem”的可绑定 DependencyProperty,它是“选定的”项目(如果有)。
它甚至可以双向工作,即您可以从 ViewModel 中重置选择。

不幸的是,据我所知,没有类似的多重选择功能。
您必须自己实现此操作,迭代选定的记录,检查它们是否是数据记录,获取记录和数据项等......

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...

盛装女皇 2024-09-12 09:37:33

尝试将 DataGrid 的 SelectedItem 绑定到视图模型中的属性。

然后,您可以在 OpenTradeHistory() 方法中访问此属性。

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

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

烟燃烟灭 2024-09-12 09:37:33

为了绑定到选定的项目,我选择使用 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();
        }
    }
}

在您看来,某些地方会有类似以下内容的内容(为了简洁起见,我省略了命名空间映射):

现在你的问题是上下文菜单上的命令绑定,那是别的问题......我会重新审视这个

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文