Pass parameter via context menu to DevExpress TreeListControl

跟風遠走 提交于 2019-12-11 05:53:04

问题


I have a TreeListControl and I will like to pass the selected row as parameter on my context menu. So my control looks like:

View:

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

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

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

all of them return null... The only parameter I have been able to pass is:

CommandParameter="Foo"

Edit

DevExpress answered the question at: 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>

This will give you the control on which you open this ContextMenu.I hope this will give you an idea.



来源:https://stackoverflow.com/questions/14775007/pass-parameter-via-context-menu-to-devexpress-treelistcontrol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!