0

as I found a lot of tasks (questions) to my question, I still have no idea how that works. Even it's a much too complex example or simply the needed namespace was missing. So after one hour of research, my question...

How do I intergrate doubleclick-command to my WPF treeview (-items)?

Actually, my tree looks like this:

<!-- used Namespace: xmlns:i="http://schemas.microsoft.com/xaml/behaviors" -->
    <TreeView DataContext="{Binding ProjectTree}" ItemsSource="{Binding ProjectNode}" DockPanel.Dock="Left" 
              x:Name="ProjectTree" Margin="0 0 2 0" Grid.Column="0">
        <TreeView.ContextMenu>
            <ContextMenu StaysOpen="True">
                <MenuItem Header="Löschen" Height="20" 
                          Command="{Binding RemoveNodeCommand}"
                          CommandParameter="{Binding ElementName=ProjectTree, Path=SelectedItem}">
                    <MenuItem.Icon>
                        <Image Source="/Icons/32x32/Remove_32x32.png" Width="15" Height="15"/>
                    </MenuItem.Icon> 
                </MenuItem>
            </ContextMenu>
        </TreeView.ContextMenu>
        
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectedItemChanged">
                <i:InvokeCommandAction Command="{Binding TreeNodeSelectedCommand}" 
                                       CommandParameter="{Binding ElementName=ProjectTree, Path=SelectedItem}"/>
            </i:EventTrigger>
            <i:EventTrigger EventName="MouseDoubleClick">
                <i:InvokeCommandAction Command="{Binding UpdateNodeCommand}" 
                                       CommandParameter="{Binding ElementName=ProjectTree, Path=SelectedItem}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>

        <TreeView.ItemContainerStyle>
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
                <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
                <Setter Property="FontWeight" Value="Normal"/>
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="FontWeight" Value="Bold"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TreeView.ItemContainerStyle>

        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                <StackPanel Orientation="Horizontal">
                    <Image Margin="3" Source="{Binding ItemType, Converter={x:Static misc:TreeItemImageConverter.Instance }}" Width="20" />
                    <TextBlock VerticalAlignment="Center" Text="{Binding Name}"/>
                </StackPanel>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

I don't really understand how the Interaction.Triggers work but should they be the right way?
I'm using the ICommand and a relaycommand for my menu- and button-commands. So far, everything fine in my viewmodels.

Sorry if that is a noob question.

EDIT As it wasn't a noob question but a noob typo, MouseDoubleClick simply works as SelectedItemChanged so my question was unneccessary. (updated code above)

Telefisch
  • 305
  • 1
  • 19

2 Answers2

3

You can add an interaction trigger in the data template.

I don't have your code so I've adapted a project I already had hanging about to show this working.

My markup, which is of course illustrative rather than cut and paste ready for your purposes:

    <TreeView Name="tv" ItemsSource="{Binding Families}" Grid.Row="1" Grid.ColumnSpan="2"
              >
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type local:Family}" ItemsSource="{Binding Members}">
                <Border>
                    <StackPanel Orientation="Horizontal"
                                Height="32"
                                >
                        <Label VerticalAlignment="Center" FontFamily="WingDings" Content="1"/>
                        <TextBlock Text="{Binding Name}" />
                    </StackPanel>
                </Border>
            </HierarchicalDataTemplate>
            <DataTemplate DataType="{x:Type local:FamilyMember}">
                <ContentControl>
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseDoubleClick">
                            <i:InvokeCommandAction Command="{Binding DataContext.TestCommand, RelativeSource={RelativeSource AncestorType=TreeView}}" 
                                                   CommandParameter="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType=TreeViewItem}}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>

                    <StackPanel Orientation="Horizontal"
                                Height="32"
                                x:Name="sp">

                        <TextBlock Tag="{Binding DataContext.Name, 
                            RelativeSource={RelativeSource AncestorType=TreeViewItem , AncestorLevel=2}
                            }" 
                            Text="{Binding Name}" />
                        <TextBlock Text="{Binding Age}" Foreground="Green" />
                    </StackPanel>
                </ContentControl>
            </DataTemplate>
        </TreeView.Resources>

    </TreeView>

The command in my window viewmodel

public class MainWindowViewModel
{
    private DelegateCommand<FamilyMember> testCommand;
    public ICommand TestCommand
    {
        get
        {
            if (testCommand == null)
            {
                testCommand = new DelegateCommand<FamilyMember>((member) =>
                {
                     MessageBox.Show($"Member's name is {member.Name}");
                });
            }
            return testCommand;
        }
    }

My tree has family and then each has family members.

If I expand members out I can double click a family member and the message box confirms the name is correct.

Note the contentcontrol in the datatemplate. You need a control to give you double click support.

Andy
  • 11,864
  • 2
  • 17
  • 20
1

Interaction triggers cannot be applied in style setters but you could create an attached behaviour that handles the MouseDoubleClick event for the TreeViewItem:

<Style TargetType="{x:Type TreeViewItem}">
    <Setter Property="local:YourBehavior.Command" Value="{Binding YourCommand}" />
    ...
</Style>

Another option is to invoke the command programmtically from the code-behind of the view.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • Thank you for your idea. I ended up in codebehind but that gave the `` a last chance. I will complete my Question after complete integration in my project. – Telefisch Jun 26 '20 at 13:41