Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果已选择行,则在wpf datagrid中右键单击时禁用行选择_C#_Wpf_Wpfdatagrid - Fatal编程技术网

C# 如果已选择行,则在wpf datagrid中右键单击时禁用行选择

C# 如果已选择行,则在wpf datagrid中右键单击时禁用行选择,c#,wpf,wpfdatagrid,C#,Wpf,Wpfdatagrid,我正在一个datagrid上工作,它允许用户选择多行。但当用户单击行的标题时,选择就会丢失 如果行已被选中,如何在右键单击时禁用行选择 我试着通过行为来做到这一点 public class DataGridRowBehavior { public static readonly DependencyProperty DisableSelectionOnRightClickProperty = DependencyProperty.RegisterAttached(

我正在一个datagrid上工作,它允许用户选择多行。但当用户单击行的标题时,选择就会丢失

如果行已被选中,如何在右键单击时禁用行选择

我试着通过行为来做到这一点

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
    以防止选择