简体   繁体   English

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

[英]Disable the row selection on right click in wpf datagrid if row is already selected

I am working on a datagrid which enable user to select multiple rows. 我正在一个数据网格上,它使用户可以选择多行。 but when user clicks on header of row the selection get lost. 但是,当用户单击行标题时,选择内容将丢失。

How can I disable the row selection on right click if the row is already selected. 如果已经选择了行,如何右键单击禁用行选择。

I tried to do it via Behavior 我试图通过行为来做到这一点

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

But this one is not working correctly as other functionality of right click is also broken. 但这不能正常工作,因为右键单击的其他功能也已损坏。 eg Context menu. 例如,上下文菜单。 The context menu is not enabling its Application Commands. 上下文菜单未启用其“应用程序命令”。

Is there any other way to disable the selection or let the selection remain as it is if I click on any of the selected row? 如果单击任何选定的行,还有其他方法可以禁用选择或让选择保持原样吗?

You have two choices: 您有两种选择:

  1. Either create custom DataGrid or DataGridRow and create SelectionChanging or Selection events. 创建自定义DataGridDataGridRow并创建SelectionChangingSelection事件。 It's needed to prevent selection. 需要防止选择。 This time, this controls have only SelectionChanged and Selected events. 这次,此控件仅具有SelectionChangedSelected事件。 Next time, I think you could write code 下次,我认为您可以编写代码

  2. If you do not want create custom controls you can create a Behavior . 如果不想创建自定义控件,则可以创建一个Behavior For example: 例如:

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

If you want, you can make this code more flexible. 如果需要,可以使此代码更灵活。 But you must understand, that you can only set e.Handled to true to prevent selection. 但是您必须了解,只能将e.Handled设置为true以防止选择。

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

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