something like:
<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>
and converter:
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();
}
}
I intentionally left out the CommandBehaviors:MouseDoubleClick.CommandParameter
from the Style
Trigger
as it does not seem to change from default. Just added a Foreground
property for testing, You can delete that too.