附加在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>

它为树视图项目定义了一些属性,但它不能完全工作-我的意思是它设置前景色,但当我双击任何项目时,它不会触发我的命令

附加在TreeViews上的属性

我想这与你上一个问题有关。

  • 使用我昨天提到的这个设置,请记住RelayCommand<T>需要位于相关元素上,而不是父VM上。这就是为什么你可能会看到Foreground属性工作良好,但不是命令执行,因为DataContext命令正在寻找(ServerItemDatabaseItem)实际上并没有定义命令ConnectServerConnectDb
  • 也不是有两个触发器,将ConnectDb设置为默认值,并使用触发器将ConnectServer设置为相关元素的ServerItem

现在要解决您的问题,您可以在DatabaseItem中的ServerItemConnectDb命令中定义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);
  }
}