【问题标题】:Add doubleclick-command to wpf-treeview-items将双击命令添加到 wpf-treeview-items
【发布时间】:2023-04-01 05:18:01
【问题描述】:

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

如何将双击命令集成到我的 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 工作,所以我的问题是不必要的。 (上面更新了代码)

【问题讨论】:

    标签: wpf xaml command


    【解决方案1】:

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

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

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

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

    我的窗口视图模型中的命令

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

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

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

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

    【讨论】:

    • 是的,它适用于选择项目,但不适用于双击
    • 我发布的标记绝对有效。您已经有一个事件触发器。添加另一个类似于我上面的标记有什么问题?
    • Oh maaaan... 看来我只是打错字了。所以&lt;i:EventTrigger EventName="MouseDoubleClick"&gt; 有效。谢谢!
    【解决方案2】:

    交互触发器cannot 应用于样式设置器,但您可以创建一个attached behaviour 来处理MouseDoubleClick 事件的TreeViewItem

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

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

    【讨论】:

    • 感谢您的想法。我最终进入了代码隐藏,但这给了&lt;i:EventTrigger EventName="MouseDoubleClick"&gt; 最后的机会。完全集成到我的项目中后,我将完成我的问题。
    猜你喜欢
    • 1970-01-01
    • 2012-01-09
    • 2016-08-01
    • 1970-01-01
    • 2010-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多