将 doubleclick-command 添加到 wpf-treeview-items

Add doubleclick-command to wpf-treeview-items

因为我发现了很多任务(问题)来解决我的问题,所以我仍然不知道它是如何工作的。 即使它是一个过于复杂的示例,或者只是缺少所需的名称空间。所以经过一个小时的研究,我的问题...

如何将 doubleclick-command 集成到我的 WPF 树视图(-items)中?

实际上,我的树是这样的:

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

我不太明白 Interaction.Triggers 是如何工作的,但它们应该是正确的方法吗?
我正在为我的菜单和按钮命令使用 ICommand 和中继命令。到目前为止,我的视图模型中的一切都很好。

抱歉,如果这是一个菜鸟问题。

编辑 因为这不是一个菜鸟问题而是一个菜鸟错字,MouseDoubleClick 简单地用作 SelectedItemChanged 所以我的问题是不必要的。 (上面的更新代码)

交互触发器 cannot be applied in style setters but you could create an attached behaviour 处理 TreeViewItemMouseDoubleClick 事件:

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

另一种选择是从视图的 以编程方式调用命令。

您可以在数据模板中添加交互触发器。

我没有你的代码,所以我改编了一个我已经挂了的项目来展示这个工作。

我的标记,这当然是说明性的,而不是为您的目的准备好的剪切和粘贴:

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

我的 window 视图模型中的命令

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

我的树有家人,然后每个人都有家人。

如果我展开成员,我可以双击一个家庭成员,消息框会确认名字是正确的。

注意数据模板中的内容控件。 您需要一个控件来为您提供双击支持。