TreeViews上的附加属性

时间:2013-06-01 09:05:10

标签: c# wpf mvvm attached-properties

所以我有这段代码:

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

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

1 个答案:

答案 0 :(得分:1)

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

  • 使用此设置,您已经提到了我昨天提到的内容,请记住RelayCommand<T>需要在相关元素上而不是父虚拟机上。这就是为什么你可能看到Foreground属性工作正常而不是命令执行,因为DataContext寻找命令(ServerItemDatabaseItem)实际上并没有定义命令ConnectServerConnectDb
  • 此外,我们不是设置两个触发器,而是将ConnectDb设置为默认值,并使用触发器为相关元素上的ConnectServer设置ServerItem

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