我正在使用此处的代码:WPF/MVVM - how to handle double-click on TreeViewItems in the ViewModel?,来自已接受的答案。我的xaml是这样的:
<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是否是根节点?
答案 0 :(得分: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.CommandParameter
Style
中的Trigger
,因为它似乎没有从默认值更改。刚刚添加了Foreground
属性进行测试,您也可以删除它。