我有一个TreeListControl,我希望将选定的行作为参数传递给我的上下文菜单。所以我的控制如下:
I have a TreeListControl and I will like to pass the selected row as parameter on my context menu. So my control looks like:
查看:
<Window x:Class="WpfApplication2.MainWindow" xmlns="schemas.microsoft/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" xmlns:dxt="schemas.devexpress/winfx/2008/xaml/grid" > <dxt:TreeListControl Name="treeList" ItemsSource="{Binding MyCollection}"> <!-- Context menu I AM HAVING TROUBLE HERE PASSING A PARAMETER TO THE COMMAND --> <dxt:TreeListControl.ContextMenu> <ContextMenu> <MenuItem Header="Mount" Command="{Binding MyCustomCommand}" CommandParameter="{Binding SELECTED_JUST_CLICKED_ROW_OF_THIS_CONTROL}"> <MenuItem.Icon> <Image Source="/Server;component/Images/SomeImage.png" Width="40" /> </MenuItem.Icon> </MenuItem> </ContextMenu> </dxt:TreeListControl.ContextMenu> <!-- Columns of the control --> <dxt:TreeListControl.Columns> <dxt:TreeListColumn FieldName="Name" Header="Name"/> <dxt:TreeListColumn FieldName="Position" Header="Position"/> </dxt:TreeListControl.Columns> <!-- View --> <dxt:TreeListControl.View> <dxt:TreeListView Name="treeListView1" AutoWidth="True" KeyFieldName="ID" ParentFieldName="ParentID"/> </dxt:TreeListControl.View> </dxt:TreeListControl> </Window>背后的代码
Code Behind
using System.Collections.Generic; using System.Windows; using System.Windows.Documents; namespace WpfApplication2 { public partial class MainWindow : Window { // Properties that are binded on View public ObservableCollection<Employee> MyCollection {get;set;} public MyCommand MyCustomCommand { get; set; } public MainWindow ( ) { InitializeComponent( ); // init properties MyCollection = new ObservableCollection<Employee>(); MyCustomCommand = new MyCommand(); // bind properties to view this.DataContext = this; // add items to observable collection foreach(var employee in GetStuff() ) MyCollection.Add( employee ); } // generate employees public static List<Employee> GetStuff ( ) { List<Employee> stuff = new List<Employee>( ); stuff.Add( new Employee() { ID = 2 , ParentID = 0 , Name = "Irma R. Marshall" } ); stuff.Add( new Employee() { ID = 6 , ParentID = 2 , Position = "Brian C. Cowling" } ); stuff.Add( new Employee() { ID = 7 , ParentID = 2 , Position = "Thomas C. Dawson" } ); return stuff; } } } public class Employee { public int ID { get; set; } public int ParentID { get; set; } public string Name { get; set; } public string Position { get; set; } } public class MyCommand : ICommand { public bool CanExecute ( object parameter ) { // Here I want parameter to be selected ROW!!!! return true; } public event EventHandler CanExecuteChanged; public void Execute ( object parameter ) { // Here I want parameter = selected row !!!! // based on value of parameter perform logic } }一切正常,但CommandParameter。我已经尝试过如下:
Everything works but the CommandParameter. I have tried things like:
CommandParameter="{Binding RowHandle.Value}" CommandParameter="{Binding Data.Name}" CommandParameter="{Binding Data}" CommandParameter="{Binding ElementName=treeList, Path=Name}"所有这些都返回null ...我能够通过的唯一参数是:
all of them return null... The only parameter I have been able to pass is:
CommandParameter="Foo"编辑
DevExpress回答了以下问题: www.devexpress/Support/Center/Question/Details/Q473660
Edit
DevExpress answered the question at: www.devexpress/Support/Center/Question/Details/Q473660
推荐答案<ContextMenu Name="MyContextMenu" Tag="{Binding PlacementTarget,RelativeSource={RelativeSource Self}}"> <MenuItem Header="Mount" Command="{Binding MyCustomCommand}" CommandParameter="{Binding Tag , ElementName= MyContextMenu}"/> </ContextMenu>
这将给你打开这个ContextMenu的控件。我希望这会给你一个想法。
This will give you the control on which you open this ContextMenu.I hope this will give you an idea.
更多推荐
通过上下文菜单将参数传递给DevExpress TreeListControl
发布评论