Question

I'm using code from here: WPF/MVVM - how to handle double-click on TreeViewItems in the ViewModel?, from accepted answer. My xaml is like this:

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

Attached command from Style tag does not work, and it should't since there is already a command attached on the TreeView tag level. What I'm trying to do is to execute one command for top level children, and another for lower level children. How do I do that?

EDIT:

So, as Viv suggested - how do I check if treeviewitem is a root node?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top