简体   繁体   English

Datagrid上下文菜单仅适用于单选

[英]Datagrid context menu only for single select

I have a datagrid with context menu. 我有一个带有上下文菜单的数据网格。 How prevent context menu showing for multiple selection? 如何防止显示上下文菜单以进行多选? I tried to add attached property but got an error The attachable property 'ContextMenuVisibilityMode' was not found in type 'ProcessIntasnceActivitiesView' 我尝试添加附加属性,但出现错误在“ ProcessIntasnceActivitiesView”类型中找不到可附加属性“ ContextMenuVisibilityMode”

namespace TiM.Windows.App.View
{
public partial class ProcessIntasnceActivitiesView
{
public static readonly DependencyProperty ContextMenuVisibilityModeProperty =
  DependencyProperty.RegisterAttached("ContextMenuVisibilityMode", typeof (string),
    typeof (ProcessIntasnceActivitiesView),
    new PropertyMetadata("Multiple", OnContextMenuVisibilityModeChanged));

private static void OnContextMenuVisibilityModeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
  var dgrd = d as DataGrid;

  if (dgrd != null)
    dgrd.ContextMenuOpening += (sender, args) =>
    {
      switch (e.NewValue.ToString())
      {
        case "Single":
          args.Handled = (sender as DataGrid)?.SelectedItems.Count > 1;
          break;
        case "Multiple":
          args.Handled = !((sender as DataGrid)?.SelectedItems.Count > 1);
          break;
      }
    };
  }
 ...

}

xmlns:local="clr-namespace:TiM.Windows.App.View"
<DataGrid ... local:ProcessIntasnceActivitiesView.ContextMenuVisibilityMode="Single">

Handle ContextMenuOpening event of DataGrid . 处理DataGrid ContextMenuOpening事件。

private void DataGrid_ContextMenuOpening_1(object sender, ContextMenuEventArgs e)
    {
        e.Handled = (sender as DataGrid).SelectedItems.Count > 1;
    }

You can put this logic into an AttachedProperty . 您可以将此逻辑放入AttachedProperty

namespace WpfStackOverflow
{
/// <summary>
/// Interaction logic for Window2.xaml
/// </summary>
public partial class Window2 : Window
{
...
// Using a DependencyProperty as the backing store for ContextMenuVisibilityMode.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ContextMenuVisibilityModeProperty =
        DependencyProperty.RegisterAttached("ContextMenuVisibilityMode", typeof(string), typeof(Window2), new PropertyMetadata("Extended", new PropertyChangedCallback(OnContextMenuVisibilityModeChanged)));

    private static void OnContextMenuVisibilityModeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        DataGrid dgrd = d as DataGrid;
        dgrd.ContextMenuOpening += ((sender, args) => {
            if (e.NewValue.ToString() == "Single")
                args.Handled = (sender as DataGrid).SelectedItems.Count > 1;
            else if (e.NewValue.ToString() == "Extended")
                args.Handled = !((sender as DataGrid).SelectedItems.Count > 1);
            else { 
                // do something
            }
        });
    }

...
}

And apply it to DataGrid : 并将其应用于DataGrid

<Window ...
xmlns:local="clr-namespace:WpfStackOverflow"
...>
<DataGrid local:Window2.ContextMenuVisibilityMode="Single"  ... />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM