Передать параметр через контекстное меню в DevExpress TreeListControl
У меня есть TreeListControl, и я хотел бы передать выбранную строку в качестве параметра в моем контекстном меню. Так что мой контроль выглядит так:
Посмотреть:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:dxt="http://schemas.devexpress.com/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>
Код позади
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. Я пробовал такие вещи, как:
CommandParameter="{Binding RowHandle.Value}"
CommandParameter="{Binding Data.Name}"
CommandParameter="{Binding Data}"
CommandParameter="{Binding ElementName=treeList, Path=Name}"
все они возвращают нуль... Единственный параметр, который мне удалось передать, это:
CommandParameter="Foo"
редактировать
DevExpress ответил на вопрос по адресу: http://www.devexpress.com/Support/Center/Question/Details/Q473660
1 ответ
Решение
<ContextMenu Name="MyContextMenu" Tag="{Binding PlacementTarget,RelativeSource={RelativeSource Self}}">
<MenuItem Header="Mount" Command="{Binding MyCustomCommand}" CommandParameter="{Binding Tag , ElementName= MyContextMenu}"/>
</ContextMenu>
Это даст вам контроль, на котором вы открываете это ContextMenu. Я надеюсь, что это даст вам идею.