0

我正在尝试将 Command 和 CommandParameter 属性附加到 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>

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

编辑:在输出窗口中出现此错误:

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')
4

1 回答 1

0

因此,经过一番阅读,我能够正确地完成这项工作。我只需要更改命令:MouseDoubleClick.Commands 值

{Binding SelectWaferCommand}"

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

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

于 2015-06-05T17:07:40.413 回答