【问题标题】:CommandParameters in ContextMenu in WPFWPF 中 ContextMenu 中的 CommandParameters
【发布时间】:2010-10-05 00:12:06
【问题描述】:

我有一个 WPF TreeView 控件,它的项目有一个HierarchicalDataTemplate。现在在HierarchicalDataTemplate 中,我有一个LabelLabel 有一个ContextMenu,其中有一个Delete 的菜单项。删除菜单项绑定到名为DeleteCommand 的命令,该命令是已设置为HierarchicalDataTemplateDataType 的类的一部分。

现在,我想在 ContextMenu 的 Delete menuitem 的 DeleteCommandCommandParameters 中传递 TreeView 控件,以便在删除当前选定的项目时处理 TreeViewItems 的选择。

但是,如果我将CommandParameters 绑定为{Binding ElementName=TreeViewName} 或其他任何内容,除非绑定的元素是DataContext 中的属性,否则它始终为空。

任何人都可以帮助我解决问题,因为我认为,我已经尝试了所有可能的东西,例如 RelativeSource 和 AncestorType 等,但它始终为空。对我来说,这看起来像是框架中的限制或错误。

【问题讨论】:

    标签: c# wpf .net-3.5


    【解决方案1】:

    问题在于 ContextMenu 位于其自己的可视化树的根部,因此任何 RelativeSource.FindAncestor 绑定都不会超过 ContextMenu。

    一种解决方案是使用PlacementTarget 属性从您的标签设置两阶段绑定:

    <Label Tag="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={
        x:Type TreeView}}}">
        <Label.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Delete" Command="{x:Static local:Commands.DeleteCommand}"
                    CommandParameter="{Binding PlacementTarget.Tag, RelativeSource={
                    RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"/>
            </ContextMenu>
        </Label.ContextMenu>
    </Label>
    

    但是,这很 hacky。您最好将 MenuItem 的 CommandTarget 属性设置为 ContextMenu 的 PlacementTarget 并在 TreeView 上使用命令处理程序。这意味着您不必传递 TreeView。

    【讨论】:

    • 很简单:
    • @JoanComas:在尝试了所有其他方法都没有成功(也不了解 FindAncestors 的含义等)之后,您的 Way easy 方法完美运行!谢谢。
    • @JoanComasFdz 的评论真的很有帮助。我可以简单地申请CommandParameter="{Binding}"
    【解决方案2】:
    <ContextMenu>
        <MenuItem Header="Edit Item"
                       Command="{Binding EditItemCommand, Mode=OneWay}"
                       CommandParameter="{Binding Path=UIElement.(views:DataGridView.SelectedItems), RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}" />
    <ContextMenu>
    

    【讨论】:

      【解决方案3】:

      看看WPF CommandParameter Binding Problem。也许它可以提供一些关于正在发生的事情的指针。

      【讨论】:

        【解决方案4】:
        <MenuItem Header="..." 
                  Command="{Binding Path=...}" 
                  CommandParameter="{Binding Path=PlacementTarget, RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType={x:Type ContextMenu}}}">
        </MenuItem>
        

        ContextMenu.PlacementTarget,是标签,菜单项所在的位置。从 Lavel 可以访问其父 Treeview。

        【讨论】:

          【解决方案5】:

          对于 DataGrid 中的每个元素

          <ContextMenu>
              <MenuItem Header="Edit Item"
                             Command="{Binding EditItemCommand, Mode=OneWay}"
                             CommandParameter="{Binding Path=PlacementTarget.SelectedItem, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}" />
          <ContextMenu>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多