我正在尝试将Command和CommandParameter属性附加到TreeViewItem,以便将鼠标双击事件路由到ViewModel中的Command。似乎双击事件永远不会被解雇。
MouseDoubleClick:
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')
答案 0 :(得分:0)
所以经过一些阅读后,我能够正确地完成这项工作。我只需要更改命令:MouseDoubleClick.Commands值
这
{Binding SelectWaferCommand}"
到
{Binding DataContext.TreeviewViewModel.SelectWaferCommand,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TreeView}}}"
因为WaferHierarchyModel对象没有SelectWaferCommand,TreeviewViewModel会这样做。这基本上告诉它执行在最近的TreeView祖先DataContext.TreeviewViewModel对象中找到的SelectWaferCommand。