使用 AttachedProperties 的 TreeViewItem 上的 MouseDoubleClick 命令出现问题

Issues with MouseDoubleClick command on TreeViewItem using AttachedProperties

我正在尝试将命令和命令参数 属性 附加到 TreeViewItem,以便我可以将鼠标双击事件路由到 ViewModel 中的命令。似乎甚至从未触发过双击事件。

鼠标双击:

namespace VisualInspectionConsole.Commands
{
    public class MouseDoubleClick
    {
        public static DependencyProperty CommandProperty =
            DependencyProperty.RegisterAttached(
                "Command",
                typeof(ICommand), 
                typeof(MouseDoubleClick),
                new UIPropertyMetadata(CommandChanged));
        public static DependencyProperty CommandParameterProperty =
            DependencyProperty.RegisterAttached(
                "CommandParameter",
                typeof(object),
                typeof(MouseDoubleClick),
                new UIPropertyMetadata(null));

        public static void SetCommand(DependencyObject target, ICommand value)
        {
            target.SetValue(CommandProperty, value);
        }  
        public static void SetCommandParameter(DependencyObject target, object value)
        {
            target.SetValue(CommandParameterProperty, value);
        }
        public static object GetCommandParameter(DependencyObject target)
        {
            return target.GetValue(CommandParameterProperty);
        }

        private static void CommandChanged(
            DependencyObject target, DependencyPropertyChangedEventArgs e)
        {
            Control control = target as Control;
            if (control != null)
            {
                if ((e.NewValue != null) && (e.OldValue == null))
                {
                    control.MouseDoubleClick += OnMouseDoubleClick;
                }
                else if ((e.NewValue == null) && (e.OldValue != null))
                {
                    control.MouseDoubleClick -= OnMouseDoubleClick;
                }
            }
        }
        private static void OnMouseDoubleClick(object sender, RoutedEventArgs e)
        {
            Control control = sender as Control;
            ((ICommand) control.GetValue(CommandProperty)).Execute(
                control.GetValue(CommandParameterProperty));
        }
    }
}

MainWindow.xaml 中的树视图代码:

<Window
...................
    xmlns:commands ="clr-namespace:VisualInspectionConsole.Commands"
...................
    <TreeView
        Width="275"
        Name="tvwWaferList"
        ScrollViewer.CanContentScroll="True"
        ScrollViewer.VerticalScrollBarVisibility="Visible"
        ItemsSource="{Binding TreeviewViewModel.WaferList}"
        SelectedValuePath="Wafer"
        Background="{StaticResource accentBrushOne}"
        Foreground="Black"
        FontFamily="SegoeUI"
        FontWeight="Bold"
        Margin="1"
        BorderBrush="{StaticResource primaryBrush}">
        <TreeView.ItemContainerStyle>
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="Foreground" Value="{StaticResource primaryBrush}"/>
                <Setter Property="FontWeight" Value="Normal"/>
                <Setter
                    Property="commands:MouseDoubleClick.Command"
                    Value="{Binding SelectWaferCommand}"/>
                <Setter
                    Property="commands:MouseDoubleClick.CommandParameter"
                    Value="{Binding}"/>
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="FontWeight" Value="Bold"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TreeView.ItemContainerStyle>
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Wafers}">
                <TextBlock
                    Text="{Binding}"
                    Foreground="Black"/>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

谁能告诉我为什么鼠标双击不起作用?

编辑:在输出中出现此错误 window:

System.Windows.Data Error: 40 : BindingExpression path error: 'SelectWaferCommand' property not found on 'object' ''WaferHierarchyModel' (HashCode=4494425)'. BindingExpression:Path=SelectWaferCommand; DataItem='WaferHierarchyModel' (HashCode=4494425); target element is 'TreeViewItem' (Name=''); target property is 'Command' (type 'ICommand')

所以在阅读之后我能够正确地完成这项工作。我只需要更改 commands:MouseDoubleClick.Commands 值

来自

{Binding SelectWaferCommand}"

{Binding DataContext.TreeviewViewModel.SelectWaferCommand, 
 RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TreeView}}}"

因为 WaferHierarchyModel 对象没有 SelectWaferCommand,TreeviewViewModel 有。这基本上告诉它执行在最近的 TreeView 祖先 DataContext.TreeviewViewModel 对象中找到的 SelectWaferCommand。