C# 如果已选择行,则在wpf datagrid中右键单击时禁用行选择
我正在一个datagrid上工作,它允许用户选择多行。但当用户单击行的标题时,选择就会丢失 如果行已被选中,如何在右键单击时禁用行选择 我试着通过行为来做到这一点C# 如果已选择行,则在wpf datagrid中右键单击时禁用行选择,c#,wpf,wpfdatagrid,C#,Wpf,Wpfdatagrid,我正在一个datagrid上工作,它允许用户选择多行。但当用户单击行的标题时,选择就会丢失 如果行已被选中,如何在右键单击时禁用行选择 我试着通过行为来做到这一点 public class DataGridRowBehavior { public static readonly DependencyProperty DisableSelectionOnRightClickProperty = DependencyProperty.RegisterAttached(
public class DataGridRowBehavior
{
public static readonly DependencyProperty DisableSelectionOnRightClickProperty = DependencyProperty.RegisterAttached(
"DisableSelectionOnRightClick",
typeof(bool),
typeof(DataGridRowBehavior),
new UIPropertyMetadata(false, OnDisableSelectionOnRightClick));
public static bool GetDisableSelectionOnRightClick(DependencyObject dgRow)
{
return (bool)dgRow.GetValue(DisableSelectionOnRightClickProperty);
}
public static void SetDisableSelectionOnRightClick(DependencyObject dgRow, bool value)
{
dgRow.SetValue(DisableSelectionOnRightClickProperty, value);
}
public static void SetListViewFocus(DependencyObject d, bool use)
{
d.SetValue(DisableSelectionOnRightClickProperty, use);
}
public static void OnDisableSelectionOnRightClick(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
DataGridRowHeader header = d as DataGridRowHeader;
header.MouseRightButtonUp += header_MouseRightButtonUp;
}
static void header_MouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
var header = sender as DataGridRowHeader;
if (header.IsRowSelected)
{
if (header.ContextMenu != null)
{
header.ContextMenu.IsOpen = true;
}
e.Handled = true;
}
}
}
但是,由于右键单击的其他功能也被破坏,这个功能无法正常工作。e、 g上下文菜单。上下文菜单未启用其应用程序命令
如果单击任何选定行,是否有其他方法来禁用选择或保持选择不变?您有两种选择:
DataGrid
或DataGridRow
并创建SelectionChanging
或Selection
事件。它需要防止选择。这一次,此控件只有SelectionChanged
和Selected
事件。下次,我想你可以写代码了行为
。例如:
public class SuppressButtonClickBehavior : Behavior<Button>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.AddHandler(UIElement.PreviewMouseLeftButtonDownEvent,
new RoutedEventHandler(OnPreviewMouseLeftButtonDown),
true);
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.RemoveHandler(UIElement.PreviewMouseLeftButtonDownEvent,
new RoutedEventHandler(OnPreviewMouseLeftButtonDown));
}
private void OnPreviewMouseLeftButtonDown(Object sender, RoutedEventArgs e)
{
e.Handled = true;
if (AssociatedObject.Command != null)
{
AssociatedObject.Command.Execute(AssociatedObject.CommandParameter);
}
}
}
公共类SuppressButtonClickBehavior:行为
{
受保护的覆盖无效附加()
{
base.onatached();
AssociatedObject.AddHandler(UIElement.PreviewMouseLeftButtonDownEvent,
新RoutedEventHandler(预览鼠标左键向下),
正确的);
}
附加时受保护的覆盖无效()
{
base.OnDetaching();
AssociatedObject.RemoveHandler(UIElement.PreviewMouseLeftButtonDownEvent,
新RoutedEventHandler(在预览MouseLeftButtonDown上);
}
PreviewMouseLeftButtonDown上的私有void(对象发送方,RoutedEventTargets e)
{
e、 已处理=正确;
if(AssociatedObject.Command!=null)
{
AssociatedObject.Command.Execute(AssociatedObject.CommandParameter);
}
}
}
e.Handled
设置为true
以防止选择