附加在TreeViews上的属性
本文关键字:属性 TreeViews | 更新日期: 2023-09-27 18:16:26
所以我有这样的代码:
<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>
它为树视图项目定义了一些属性,但它不能完全工作-我的意思是它设置前景色,但当我双击任何项目时,它不会触发我的命令
我想这与你上一个问题有关。
- 使用我昨天提到的这个设置,请记住
RelayCommand<T>
需要位于相关元素上,而不是父VM上。这就是为什么你可能会看到Foreground
属性工作良好,但不是命令执行,因为DataContext
命令正在寻找(ServerItem
和DatabaseItem
)实际上并没有定义命令ConnectServer
和ConnectDb
。 - 也不是有两个触发器,将
ConnectDb
设置为默认值,并使用触发器将ConnectServer
设置为相关元素的ServerItem
。
现在要解决您的问题,您可以在DatabaseItem
中的ServerItem
和ConnectDb
命令中定义ConnectServer
命令,或者如果您想将命令保留在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>
更新:(修复TreeViewItem
的递归DoubleClick
事件调用)
在你的班级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);
}
}