通过上下文菜单将参数传递给DevExpress TreeListControl

编程入门 行业动态 更新时间:2024-10-27 04:32:02
本文介绍了通过上下文菜单将参数传递给DevExpress TreeListControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

本文发布于:2023-11-23 02:20:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619729.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上下文   菜单   参数   TreeListControl   DevExpress

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!