ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

devexpress gridControl增加右键事件

2021-10-18 18:03:26  阅读:268  来源: 互联网

标签:xmlns http devexpress xaml com 右键 winfx gridControl schemas


定义命令:

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

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

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

 

标签:xmlns,http,devexpress,xaml,com,右键,winfx,gridControl,schemas
来源: https://www.cnblogs.com/wjx-blog/p/15421582.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有