1

所以我有这个代码:

<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>

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

4

1 回答 1

1

我认为这与您之前的问题有关。

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

现在要解决您的问题,您可以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>

更新:(修复's的递归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);
  }
}
于 2013-06-01T11:16:54.713 回答