devexpress gridControl增加右键事件

定义命令:

public ICommand CommandShowFileInFolder { get; private set; }

初始化命令:

备注:一定要放在构造函数里,如果放在page_load方法无法实现。

        public MainWindow()
        {
            CommandShowFileInFolder = new DelegateCommand<object>(ShowFileInFolder);
            InitializeComponent();
        }

实现方法:

复制代码
        void ShowFileInFolder(object parameter)
        {
            if (parameter is int)
            {
                int rowHandle = Convert.ToInt32(parameter);
                string filePath = grid1.GetCellValue(rowHandle, "Id").ToString();
                MessageBox.Show(filePath);
            }
        }
复制代码

为了方法里能得到对应的值,还需要建两个变量来存放,当前选中的信息

注意下面的MainWindow 需要填写当前窗体的名称

        public static readonly DependencyProperty CellMenuInfoProperty = DependencyPropertyManager.Register("CellMenuInfo", typeof(GridCellMenuInfo), typeof(MainWindow), new FrameworkPropertyMetadata(null));
        public static readonly DependencyProperty SelectRowInfoProperty = DependencyPropertyManager.Register("SelectRowInfo", typeof(ProjectInfo), typeof(MainWindow), new FrameworkPropertyMetadata(null));
复制代码
        public GridCellMenuInfo CellMenuInfo
        {
            get { return (GridCellMenuInfo)GetValue(CellMenuInfoProperty); }
            set { SetValue(CellMenuInfoProperty, value); }
        }
        public ProjectInfo SelectRowInfo
        {
            get { return (ProjectInfo)GetValue(SelectRowInfoProperty); }
            set { SetValue(SelectRowInfoProperty, value); }
        }
复制代码

前台代码

复制代码
        <dxg:GridControl x:Name="grid1" AutoExpandAllGroups="True" ShowBorder="False">
            <dxg:GridColumn Width="*" FieldName="Name" Header="名称"/>
            <dxg:GridColumn Width="150" FieldName="CreatedTime"  Header="创建时间" HorizontalHeaderContentAlignment="Center">
                <dxg:GridColumn.EditSettings>
                    <dxe:ButtonEditSettings DisplayFormat="yyyy-mm-dd HH:mm:ss" HorizontalContentAlignment="Center"/>
                </dxg:GridColumn.EditSettings>
            </dxg:GridColumn>
            <dxg:GridColumn FieldName="Column1" SortOrder="Ascending" SortIndex="0">
                <dxg:GridColumn.EditSettings>
                    <dxe:ButtonEditSettings/>
                </dxg:GridColumn.EditSettings>
            </dxg:GridColumn>

            <dxg:GridControl.View>
                <dxg:TableView x:Name="tableView"
                               ShowGridMenu="tableView_ShowGridMenu"
                               FocusedRowChanged="tableView_FocusedRowChanged"
                                   AllowEditing="False"
                                   LeftGroupAreaIndent="0"
                                   ShowSearchPanelMode="Never"
                                   SearchPanelNullText=""
                                   ShowGroupPanel="False"
                                   ShowIndicator="True"
                                   ShowVerticalLines="True"
                                   CompactPanelShowMode="Always"
                                   SwitchToCompactModeWidth="500"
                                   RowMinHeight="30"
                                   NavigationStyle="Row">
                    <dxg:TableView.RowCellMenuCustomizations>
                        <dxb:BarButtonItem Name="showFileInFolder"
                                           Content="查看文件"
                                           Command="{Binding ElementName=testWindow,Path=CommandShowFileInFolder}"
                                           CommandParameter="{Binding ElementName=testWindow, Path=CellMenuInfo.Row.RowHandle.Value}" />
                    </dxg:TableView.RowCellMenuCustomizations>
                </dxg:TableView>
            </dxg:GridControl.View>
复制代码

上面的“testWindow”是当前页面的名称,这个名称是前台定义的,如下:

复制代码
<Window x:Class="WpfApp3.MainWindow"
                      Name="testWindow"
                      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                      xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
                      xmlns:dxdb="http://schemas.devexpress.com/winfx/2008/xaml/demobase"
                      xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
                      xmlns:dxei="http://schemas.devexpress.com/winfx/2008/xaml/editors/internal"
                      xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
                      xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
                      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
        mc:Ignorable="d"
                      HorizontalAlignment="Stretch"
                      VerticalAlignment="Stretch"
                      d:DesignHeight="400"
                      d:DesignWidth="800"
        Title="MainWindow" Height="450" Width="1200" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded">
复制代码

 

有两个事件:tableView_ShowGridMenu和tableView_FocusedRowChanged

对应后台的代码如下:

复制代码
        private void tableView_ShowGridMenu(object sender, GridMenuEventArgs e)
        {
            CellMenuInfo = e.MenuType == GridMenuType.RowCell ? (GridCellMenuInfo)e.MenuInfo : null;
        }

        private void tableView_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            SelectRowInfo = (ProjectInfo)e.NewRow;
        }
复制代码

 

posted @   星星c#  阅读(326)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示