所以我有这段代码:
<Style TargetType="{x:Type TreeViewItem}">
<Style.Resources>
<Converters:GetElementTypeConverter x:Key="GetElementTypeConverter" />
</Style.Resources>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=., Converter={StaticResource GetElementTypeConverter}}" Value="{x:Type Models:ServerItem}">
<Setter Property="CommandBehaviors:MouseDoubleClick.Command" Value="{Binding ConnectServer}" />
<Setter Property="CommandBehaviors:MouseDoubleClick.CommandParameter" Value="{Binding Path=SelectedItem, RelativeSource={RelativeSource Self}}" />
<Setter Property="Foreground" Value="Gray" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=., Converter={StaticResource GetElementTypeConverter}}" Value="{x:Type Models:DatabaseItem}">
<Setter Property="CommandBehaviors:MouseDoubleClick.Command" Value="{Binding ConnectDb}" />
<Setter Property="CommandBehaviors:MouseDoubleClick.CommandParameter" Value="{Binding Path=SelectedItem, RelativeSource={RelativeSource Self}}" />
<Setter Property="Foreground" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
它定义了树视图项的一些属性,但它不能完全起作用 - 我的意思是它设置了前景色,但是当我双击任何项时,它不会触发我的命令。
答案 0 :(得分:1)
我认为这与您之前提出的问题有关。
RelayCommand<T>
需要在相关元素上而不是父虚拟机上。这就是为什么你可能看到Foreground
属性工作正常而不是命令执行,因为DataContext
寻找命令(ServerItem
和DatabaseItem
)实际上并没有定义命令ConnectServer
和ConnectDb
。ConnectDb
设置为默认值,并使用触发器为相关元素上的ConnectServer
设置ServerItem
。现在要解决您的问题,您可以在ConnectServer
中的ServerItem
和ConnectDb
命令中定义DatabaseItem
命令,或者如果要将命令保留在DataContext中在TreeView
中,使用RelativeSource
绑定从TreeView
中查找这些命令。
类似的东西(经过测试并且工作正常):
<TreeView ItemsSource="{Binding ServerItems}">
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Style.Resources>
<CommandBehaviors:GetElementTypeConverter x:Key="GetElementTypeConverter" />
</Style.Resources>
<Setter Property="CommandBehaviors:MouseDoubleClick.Command"
Value="{Binding Path=DataContext.ConnectDb,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type TreeView}}}" />
<Setter Property="CommandBehaviors:MouseDoubleClick.CommandParameter"
Value="{Binding Path=.}" />
<Setter Property="Foreground"
Value="Black" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=.,
Converter={StaticResource GetElementTypeConverter}}"
Value="{x:Type CommandBehaviors:ServerItem}">
<Setter Property="CommandBehaviors:MouseDoubleClick.Command"
Value="{Binding Path=DataContext.ConnectServer,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type TreeView}}}" />
<Setter Property="Foreground"
Value="Tomato" />
</DataTrigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Databases}">
<TextBlock Text="{Binding}" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
更新(修复了DoubleClick
的递归TreeViewItem
事件调用)
在您的班级MouseDoubleClick
开关:
public class MouseDoubleClick {
...
private static void OnMouseDoubleClick(object sender, RoutedEventArgs e) {
Control control = sender as Control;
ICommand command = (ICommand)control.GetValue(CommandProperty);
object commandParameter = control.GetValue(CommandParameterProperty);
command.Execute(commandParameter);
}
}
到
public class MouseDoubleClick {
...
private static void OnMouseDoubleClick(object sender, RoutedEventArgs e) {
Control control = sender as Control;
var possibleTreeViewItem = sender as TreeViewItem;
if (control == null || (possibleTreeViewItem != null && !possibleTreeViewItem.IsSelected))
return;
ICommand command = (ICommand)control.GetValue(CommandProperty);
object commandParameter = control.GetValue(CommandParameterProperty);
command.Execute(commandParameter);
}
}