【问题标题】:How to pass selectedItem of Treeview item in CommandParameter of contextmenu如何在 contextmenu 的 CommandParameter 中传递 Treeview 项的 selectedItem
【发布时间】:2017-02-20 05:54:10
【问题描述】:

我有下面的 xaml 到带有上下文菜单“编辑”的树视图控件。当我选择编辑上下文菜单时,我的 EditCommand 方法在 MVVM 中执行。

问题:

我得到的参数是“TreeView”。我想将参数作为选定的项目(使用人民币的地方)

 <TreeView x:Name="treeView" ItemsSource="{Binding TreeViewItems}">
   <TreeView.ContextMenu>
     <ContextMenu>
         <MenuItem Header="Edit" Command="{Binding EditCommand}" 
              CommandParameter="{Binding PlacementTarget, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ContextMenu}}}"/>
     </ContextMenu>
  </TreeView.ContextMenu>
</TreeView>

任何人都可以指导我在 CommandParameter 中进行哪些更改以获取所选项目。

我已经尝试过下面的链接,但提供的解决方案对我不起作用。 [WPF treeview contextmenu command parameter [CommandParameters in ContextMenu in WPF

【问题讨论】:

    标签: mvvm treeview


    【解决方案1】:

    只需像这样将SelectedItem 添加到PlacementTarget

    <TreeView x:Name="treeView" ItemsSource="{Binding TreeViewItems}">
       <TreeView.ContextMenu>
          <ContextMenu>
              <MenuItem Header="Edit" Command="{Binding EditCommand}" 
                  CommandParameter="{Binding PlacementTarget.SelectedItem, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ContextMenu}}}"/>
          </ContextMenu>
       </TreeView.ContextMenu>
    </TreeView>
    

    更新 1

    ICommand 实现:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Input;
    
    namespace WpfApplication1
    {
      public class Command<T> : ICommand
      {
        private Action<T> _execute;
        private Predicate<T> _canExecute;
    
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    
        public Command(Action<T> execute, Predicate<T> canExecute = null)
        {
            _execute = execute;
            _canExecute = canExecute;
        }
        public bool CanExecute(object parameter)
        {
            if (_canExecute == null) return true;
            return _canExecute((T)parameter);
        }
    
        public void Execute(object parameter)
        {
            _execute((T)parameter);
        }
      }
    }
    

    后面的代码:

    using System.Collections.Generic;
    using System.Windows;
    using System.Linq;
    using System;
    using System.Collections.ObjectModel;
    using System.Windows.Input;
    using WpfApplication1;
    
    namespace WPF_Sandbox
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public ObservableCollection<string> Data { get; set; }  = new ObservableCollection<string>();
            public MainWindow()
            {
                InitializeComponent();
                DataContext = this;
                Data.Add("A");
                Data.Add("B");
                Data.Add("C");
            }
    
            public ICommand EditCommand
            {
                get
                {
                    return new Command<object>(Edit);
                }
            }
    
            private void Edit(object param)
            {
                //Your code here
            }
        }    
    }
    

    这对我有用。

    【讨论】:

    • 不走运。我仍然在 EditCommand 中获得 Null 值。您能否验证一下,我是否遗漏了什么。
    • @dhiraj sakariya - 愚蠢的问题,您是否确保先左键单击一个项目以选择它,然后单击右键?如果你不是,那么这就解释了为什么你得到空参数。如果是,那么我需要查看您的 ICommand 实现。我已经更新了我的答案,以显示我的命令实现以及我的代码。注意:我通常不会在代码后面放置任何代码。相反,我将要绑定的代码放在 ViewModel 中。
    • 出色的诊断!我刚刚右键单击项目所以它没有被选中。 1.如何在commandParameter中获得右键单击的项目? 2. 我在树视图中为每个节点创建了用户控件,用户控件依赖于模板。代码sn-p:
    • 我在用户控件中创建了上下文菜单,如何将 EditCommand 绑定到 TreeView 的 DataContext 的视图模型?单击的项目数据如何作为参数传递?
    猜你喜欢
    • 2012-08-23
    • 1970-01-01
    • 2014-04-22
    • 1970-01-01
    • 2012-12-14
    • 2016-11-24
    • 2011-10-29
    • 2016-04-27
    • 2011-02-20
    相关资源
    最近更新 更多