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

时间:2016-10-08 15:20:09

标签: c# wpf

我有一个带上下文菜单的数据网格。如何防止上下文菜单显示多个选择?我尝试添加附加属性但出现错误可附加属性' ContextMenuVisibilityMode'未在类型' ProcessIntasnceActivitiesView'

中找到
sklearn/feature_extraction/hashing.py

1 个答案:

答案 0 :(得分:0)

处理ContextMenuOpening的{​​{1}}事件。

DataGrid

您可以将此逻辑放入private void DataGrid_ContextMenuOpening_1(object sender, ContextMenuEventArgs e) { e.Handled = (sender as DataGrid).SelectedItems.Count > 1; }

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

DataGrid