鼠标双击DataGrid行数据

3

我有一个DataGrid样式模板,希望添加双击行为。绑定应该是正确的,但我似乎无法让xaml编译/工作。

添加到IDictionary中的所有对象都必须具有Key属性或与其关联的某些其他类型的键。

以下代码有什么问题?

<Style TargetType="{x:Type DataGridRow}">
    <EventSetter Event="MouseDoubleClick" Handler="{Binding Connect}"/>

根据维克托的评论进行更新(会给出完全相同的错误):
<Style x:Key="dataGridRowStyle" TargetType="{x:Type DataGridRow}">
    <EventSetter Event="PreviewMouseDoubleClick" Handler="{Binding Connect}"/>

这是针对ListView的,我的代码几乎相同,但我仍然收到了那个错误... - Chris
好的...这个问题可能出在其他地方。为那个样式提供x:Key。就没问题了。 - Kapitán Mlíko
谢谢,但这仍然给我带来了完全相同的错误... - Chris
哇,这是我的错...现在我刚刚发现,TargetType应该足以解决这种类型的错误。抱歉。但是这两行代码看起来真的很好。我发现许多问题都有这两行完全相同的代码。例如这里,除了绑定之外都是一样的。但是如果你在eventsetter中绑定到命令,我认为应该以不同的方式完成。这里 - Kapitán Mlíko
我之前看过那些,似乎需要自定义代码... 我选择了另一种方法,即使用本地的双击处理程序,然后在传回的DataGridRow.Item上引发命令,虽然不完美但足够接近.. 谢谢 - Chris
3个回答

6
可以使用DataGrid输入绑定来实现目标:
<DataGrid.InputBindings>
   <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding SomeCommand}" />
</DataGrid.InputBindings>

3

您可以在数据网格行上应用以下行为,并按照使用方法进行实现。

双击行为

    public class DoubleClickBehavior
    {
        #region DoubleClick

        public static DependencyProperty OnDoubleClickProperty = DependencyProperty.RegisterAttached(
            "OnDoubleClick",
            typeof(ICommand),
            typeof(DoubleClickBehavior),
            new UIPropertyMetadata(DoubleClickBehavior.OnDoubleClick));

        public static void SetOnDoubleClick(DependencyObject target, ICommand value)
        {
            target.SetValue(OnDoubleClickProperty, value);
        }

        private static void OnDoubleClick(DependencyObject target, DependencyPropertyChangedEventArgs e)
        {
            var element = target as Control;
            if (element == null)
            {
                throw new InvalidOperationException("This behavior can be attached to a Control item only.");
            }

            if ((e.NewValue != null) && (e.OldValue == null))
            {
                element.MouseDoubleClick += MouseDoubleClick;
            }
            else if ((e.NewValue == null) && (e.OldValue != null))
            {
                element.MouseDoubleClick -= MouseDoubleClick;
            }
        }

        private static void MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            UIElement element = (UIElement)sender;
            ICommand command = (ICommand)element.GetValue(OnDoubleClickProperty);
            command.Execute(null);
        }

        #endregion DoubleClick
    }

使用方法

        <Style   BasedOn="{StaticResource {x:Type DataGridRow}}"
               TargetType="{x:Type DataGridRow}">
            <Setter Property="Helpers:DoubleClickBehavior.OnDoubleClick" Value="{Binding Path=DataContext.MyCommandInVM, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ViewLayer:MyUserControl}}}" />
        </Style>

2

不确定您是否采用MVVM模式,但是我使用附加命令行为实现了此功能,将双击事件与视图模型中的命令连接起来(其中“command”是对我的attachedCommandBehavior程序集/类的引用):

<DataGrid.RowStyle>
        <Style TargetType="{x:Type DataGridRow}">
            <Setter Property="command:CommandBehavior.Event" Value="MouseDoubleClick"/>
            <Setter Property="command:CommandBehavior.Command" Value="{Binding SelectItemCmd}"/>
            <Setter Property="command:CommandBehavior.CommandParameter" Value="{Binding }"/>
        </Style>
</DataGrid.RowStyle>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接