3

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

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

下面的代码有什么问题?

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

根据 Viktor 的评论更新(给出完全相同的错误):

<Style x:Key="dataGridRowStyle" TargetType="{x:Type DataGridRow}">
    <EventSetter Event="PreviewMouseDoubleClick" Handler="{Binding Connect}"/>
4

3 回答 3

6

可以使用 DataGrid InputBindings 来实现目标:

<DataGrid.InputBindings>
   <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding SomeCommand}" />
</DataGrid.InputBindings>
于 2013-07-19T09:43:59.493 回答
2

不确定您是否要走 MVVM 路线,但我已经使用附加命令行为将双击事件连接到我的视图模型中的命令(其中“命令”是对我的附加命令行为程序集/类的引用)来实现此功能):

<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>
于 2013-05-14T16:38:27.580 回答
2

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

双击行为

    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>
于 2013-05-14T11:11:19.717 回答