【问题标题】:DataGrid does not execute double click event on editable cellsDataGrid 不在可编辑单元格上执行双击事件
【发布时间】:2021-06-03 21:51:12
【问题描述】:

目标

目标是在选定行上双击执行命令。

问题

当我双击一个单元格时,它进入编辑模式并且不执行命令。

如果我双击 Data 列右侧的空单元格 - 它会执行命令。

另外,如果我将IsReadOnly 设置为true,它可以工作.. 它只是不适用于可编辑单元格。

问题

为什么双击事件对可编辑单元格不起作用?

重现问题的代码

XAML

<DataGrid ItemsSource="{Binding SampleModels}" SelectedItem="{Binding SelectedItem}" AutoGenerateColumns="True" >
    <DataGrid.InputBindings>
        <MouseBinding Gesture="LeftDoubleClick" Command="{Binding Command}" />
    </DataGrid.InputBindings>
</DataGrid>

型号

public class SampleModel
{
    public int Id { get; set; }
    public string Data { get; set; }
}

查看模型

public class SampleViewModel : BaseViewModel
{
    public ObservableCollection<SampleModel> SampleModels { get; set; }

    private SampleModel _selectedItem;

    public SampleModel SelectedItem
    {
        get { return _selectedItem; }
        set
        {
            _selectedItem = value;
            OnPropertyChanged();
        }
    }

    private void LoadData()
    {
        if(SampleModels == null)
        {
            SampleModels = new ObservableCollection<SampleModel>();
        }
        SampleModels.Clear();

        SampleModels.Add(new SampleModel { Id = 1, Data = "Item 1" });
        SampleModels.Add(new SampleModel { Id = 2, Data = "Item 2" });
    }

    public ICommand Command { get; }
    private void TestMethod()
    {
        var a = SelectedItem;
    }


    public SampleViewModel()
    {
        LoadData();
        Command = new RelayCommand(param => TestMethod());
    }

}

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:

    为什么双击事件对可编辑单元格不起作用?

    因为DataGrid控件通过进入单元格的编辑模式来处理双击。您还能如何编辑单元格?

    您可以使用附加行为处理单元格中的双击:

    public static class DoubleClickBehavior
    {
        public static ICommand GetCommand(UIElement element) =>
            (ICommand)element.GetValue(CommandProperty);
    
        public static void SetCommand(UIElement element, ICommand value) =>
            element.SetValue(CommandProperty, value);
    
        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.RegisterAttached(
            "Command",
            typeof(ICommand),
            typeof(DoubleClickBehavior),
            new UIPropertyMetadata(null, OnChanged));
    
        private static void OnChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            UIElement element = (UIElement)d;
            if (e.NewValue is ICommand command)
            {
                element.PreviewMouseLeftButtonDown += OnMouseLeftButtonDown;
            }
            else
            {
                element.PreviewMouseLeftButtonDown -= OnMouseLeftButtonDown;
            }
        }
    
        private static void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                UIElement element = (UIElement)sender;
                ICommand command = GetCommand(element);
                if (command != null)
                    command.Execute(null);
            }
        }
    }
    

    示例用法

    <DataGrid ItemsSource="{Binding SampleModels}" SelectedItem="{Binding SelectedItem}" AutoGenerateColumns="True" >
        <DataGrid.InputBindings>
            <MouseBinding Gesture="LeftDoubleClick" Command="{Binding Command}" />
        </DataGrid.InputBindings>
        <DataGrid.CellStyle>
            <Style TargetType="DataGridCell">
                <Setter Property="local:DoubleClickBehavior.Command"
                        Value="{Binding DataContext.Command,RelativeSource={RelativeSource AncestorType=DataGrid}}" />
            </Style>
        </DataGrid.CellStyle>
    </DataGrid>
    

    【讨论】:

      猜你喜欢
      • 2014-04-24
      • 1970-01-01
      • 2011-03-18
      • 2016-01-08
      • 1970-01-01
      • 1970-01-01
      • 2013-01-11
      • 1970-01-01
      • 2023-01-14
      相关资源
      最近更新 更多