zoukankan      html  css  js  c++  java
  • 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;
            }
  • 相关阅读:
    【Opencv】直方图函数 calchist()
    【Lintcode】070.Binary Tree Level Order Traversal II
    【C++】*p++ = *p不同环境下操作不同
    【Python】String 字符串
    【Python】File IO
    栈和队列
    链表
    printf 的格式
    Linux的基本指令--
    机器人的运动范围
  • 原文地址:https://www.cnblogs.com/wjx-blog/p/15421582.html
Copyright © 2011-2022 走看看