1

我有一个 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}"

他们都返回null ...我能够传递的唯一参数是:

CommandParameter="Foo"

编辑

DevExpress 回答了这个问题: http: //www.devexpress.com/Support/Center/Question/Details/Q473660

4

1 回答 1

1
<ContextMenu Name="MyContextMenu" Tag="{Binding PlacementTarget,RelativeSource={RelativeSource Self}}">
            <MenuItem Header="Mount" Command="{Binding MyCustomCommand}" CommandParameter="{Binding Tag , ElementName= MyContextMenu}"/>
        </ContextMenu>

这将为您提供打开此 ContextMenu 的控件。我希望这会给您一个想法。

于 2013-02-08T16:18:39.727 回答