2

我正在使用这里的代码:WPF/MVVM - 如何处理在 ViewModel 中双击 TreeViewItems?,来自接受的答案。我的 xml 是这样的:

<TreeView CommandBehaviors:MouseDoubleClick.Command="{Binding ConnectServer}" CommandBehaviors:MouseDoubleClick.CommandParameter="{Binding Path=SelectedItem, RelativeSource={RelativeSource Self}}" 
                                  Grid.Column="0" HorizontalAlignment="Stretch" DockPanel.Dock="Left" ItemsSource="{Binding Path=ServerItems, UpdateSourceTrigger=PropertyChanged}">
    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="CommandBehaviors:MouseDoubleClick.Command" Value="{Binding ConnectDb}"/>
            <Setter Property="CommandBehaviors:MouseDoubleClick.CommandParameter" Value="{Binding Path=SelectedItem, RelativeSource={RelativeSource Self}}"/>
        </Style>
    </TreeView.ItemContainerStyle>
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Databases}">
            <TextBlock Text="{Binding}" />
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

来自 Style 标记的附加命令不起作用,因为 TreeView 标记级别上已经附加了一个命令,所以它不应该起作用。我要做的是为顶级孩子执行一个命令,为低级孩子执行另一个命令。我怎么做?

编辑:

所以,正如 Viv 所建议的 - 我如何检查 treeviewitem 是否是根节点?

4

1 回答 1

2

就像是:

<Window.Resources>
  <CommandBehaviors:IsRootNodeConverter x:Key="IsRootNodeConverter" />
</Window.Resources>
<Grid>
  <TreeView>
    <TreeView.ItemContainerStyle>
      <Style TargetType="{x:Type TreeViewItem}">
        <Setter Property="CommandBehaviors:MouseDoubleClick.Command"
                Value="{Binding ConnectDb}" />
        <Setter Property="CommandBehaviors:MouseDoubleClick.CommandParameter"
                Value="{Binding Path=SelectedItem,
                                RelativeSource={RelativeSource Self}}" />
        <Setter Property="Foreground"
                Value="Black" />
        <Style.Triggers>
          <DataTrigger Binding="{Binding Path=.,
                                          RelativeSource={RelativeSource Self},
                                          Converter={StaticResource IsRootNodeConverter}}"
                        Value="True">
            <Setter Property="CommandBehaviors:MouseDoubleClick.Command"
                    Value="{Binding ConnectServer}" />
            <Setter Property="Foreground"
                    Value="Tomato" />
          </DataTrigger>
        </Style.Triggers>
      </Style>
    </TreeView.ItemContainerStyle>
    <TreeView.ItemTemplate>
      <HierarchicalDataTemplate>
        <!--<TreeViewItem>-->
          <TextBlock Text="{Binding}" />
        <!--</TreeViewItem>-->
      </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
    <TreeViewItem Header="1">
      <TreeViewItem Header="A" />
      <TreeViewItem Header="B">
        <TreeViewItem Header="AA" />
        <TreeViewItem Header="AB" />
        <TreeViewItem Header="AC" />
      </TreeViewItem>
      <TreeViewItem Header="C" />
    </TreeViewItem>
  </TreeView>
</Grid>

和转换器:

class IsRootNodeConverter : IValueConverter
{
  public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
    TreeViewItem item = value as TreeViewItem;
    if (item == null || item.Parent == null)
      return false;
    return !Object.ReferenceEquals(value.GetType(), item.Parent.GetType());
  }

  public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
    throw new NotImplementedException();
  }
}

我故意省略了CommandBehaviors:MouseDoubleClick.CommandParameterStyle Trigger因为它似乎没有从默认值改变。刚刚添加了一个Foreground用于测试的属性,您也可以将其删除。

于 2013-05-31T14:11:54.890 回答