C# Devexpress 中GridControl 自定义汉化右击菜单项

该博客介绍了如何在C#中使用DevExpress的GridControl自定义汉化右键菜单项,包括在不同类型的菜单(如列、组、总计)中更改菜单项的显示文字,并添加了导出Excel的功能。

  //所存文件路径

  private static string Path = Application.StartupPath + "//GridLayOutConfig//";

 

         /// <summary>
        /// 自定义右击菜单项
        /// </summary>
        /// <param name="gridView"></param>
        /// <param name="fileName"></param>
        public static void ResotreOptionMenu(GridView gridView, string fileName)
        {
            if (!string.IsNullOrEmpty(fileName))
                gridView.Tag = Path + fileName + "ORG";
            gridView.ShowGridMenu += new GridMenuEventHandler(GridMenuShowAddInChinese); //追加ShowGridMenu事件

        }
        /// <summary>
        /// 改写ShowGridMenu事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected static void GridMenuShowAddInChinese(object sender, GridMenuEventArgs e)
        {
            if (e.MenuType == GridMenuType.Column && (e.HitInfo.InGroupPanel || e.HitInfo.InFilterPanel))
            {
                e.Allow = false;
                return;
            }
            GridViewMenu temp = e.Menu;

            if (e.MenuType == GridMenuType.Column)
            {
                if (e.HitInfo.Column == null)
                    return;
                e.Menu.Items[0].Caption = "降序排列";
                e.Menu.Items[1].Caption = "升序排列";
                e.Menu.Items[2].Caption = "清除排序";
                e.Menu.Items[3].Caption = "按此列分组";
                e.Menu.Items[4].Caption = e.Menu.View.OptionsView.ShowGroupPanel ? "隐藏分组面板" : "显示分组面板";
                e.Menu.Items[5].Caption = "移除此列";
                e.Menu.Items[6].Caption = "选择列";
                e.Menu.Items[7].Caption = "列宽自动调整";
                e.Menu.Items[8].Caption = "列宽自动调整(所有)";
                e.Menu.Items[9].Caption = "过滤设置";
                DXMenuItem dxMenuItem = null;
                if (e.Menu.View.Tag != null)
                {
                    dxMenuItem = new DXMenuItem("恢复原有样式");
                    dxMenuItem.Click += new EventHandler(delegate(object sd, EventArgs ce)
                    {

                        string orgPath = e.Menu.View.Tag.ToString();
                        if (File.Exists(orgPath))
                        {
                            e.Menu.View.RestoreLayoutFromXml(orgPath, OptionsLayoutBase.FullLayout);
                        }
                    });

                    e.Menu.Items.Add(dxMenuItem);
                }
                dxMenuItem = new DXMenuItem(temp.View.GroupFooterShowMode == GroupFooterShowMode.Hidden ? "显示组页脚" : "隐藏组页脚");
                dxMenuItem.Click += new EventHandler(delegate(object sd, EventArgs ce)
                {
                    if (temp.View.GroupFooterShowMode == GroupFooterShowMode.Hidden)
                        temp.View.GroupFooterShowMode = GroupFooterShowMode.VisibleIfExpanded;
                    else
                        temp.View.GroupFooterShowMode = GroupFooterShowMode.Hidden;
                });
                e.Menu.Items.Add(dxMenuItem);
                dxMenuItem = new DXMenuItem("导出Excel");
                dxMenuItem.Click += new EventHandler(delegate(object sd, EventArgs ce)
                    {
                        SaveFileDialog save = new SaveFileDialog();
                        save.Title = "导出Excel";
                        save.Filter = "Excel文件(*.xls)|*.xls";
                        if (save.ShowDialog() == DialogResult.OK)
                        {
                            (sender as DevExpress.XtraGrid.Views.Grid.GridView).ExportToExcelOld(save.FileName);
                        }
                    });
                e.Menu.Items.Add(dxMenuItem);
            }
            else if (e.MenuType == GridMenuType.Group)
            {
                e.Menu.Items[0].Caption = "全部展开";
                e.Menu.Items[1].Caption = "全部折叠";
                e.Menu.Items[2].Caption = "清除分组";
                e.Menu.Items[3].Caption = "隐藏面板";
            }
            else if (e.MenuType == GridMenuType.Summary)
            {
                e.Menu.Items[0].Caption = "和";
                e.Menu.Items[1].Caption = "最小值";
                e.Menu.Items[2].Caption = "最大值";
                e.Menu.Items[3].Caption = "数量";
                e.Menu.Items[4].Caption = "平均值";
                e.Menu.Items[5].Caption = "无";
                DXMenuItem dxMenuItem = new DXMenuItem("隐藏");
                dxMenuItem.Click += new EventHandler(delegate(object sd, EventArgs ce)
                {
                    temp.View.GroupFooterShowMode = GroupFooterShowMode.Hidden;
                });
                e.Menu.Items.Add(dxMenuItem);
            }
        }
        /// <summary>
        /// 简单的汉化GridView
        /// </summary>
        /// <param name="gridView"></param>
        public static void ResotreOptionMenu(GridView gridView)
        {
            gridView.ShowGridMenu += new GridMenuEventHandler(GridMenuShowInChinese);
        }
        /// <summary>
        /// 改写ShowGridMenu事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected static void GridMenuShowInChinese(object sender, GridMenuEventArgs e)
        {
            if (e.MenuType == GridMenuType.Column && (e.HitInfo.InGroupPanel || e.HitInfo.InFilterPanel))
            {
                e.Allow = false;
                return;
            }
            GridViewMenu temp = e.Menu;
            if (e.MenuType == GridMenuType.Column)
            {
                if (e.HitInfo.Column == null)
                    return;
                e.Menu.Items[0].Caption = "降序排列";
                e.Menu.Items[1].Caption = "升序排列";
                e.Menu.Items[2].Caption = "清除排序";
                e.Menu.Items[3].Caption = "按此列分组";
                e.Menu.Items[4].Caption = e.Menu.View.OptionsView.ShowGroupPanel ? "隐藏分组面板" : "显示分组面板";
                e.Menu.Items[5].Caption = "移除此列";
                e.Menu.Items[6].Caption = "选择列";
                e.Menu.Items[7].Caption = "列宽自动调整";
                e.Menu.Items[8].Caption = "列宽自动调整(所有)";
                e.Menu.Items[9].Caption = "过滤设置";
            }
            else if (e.MenuType == GridMenuType.Group)
            {

                e.Menu.Items[0].Caption = "全部展开";
                e.Menu.Items[1].Caption = "全部折叠";
                e.Menu.Items[2].Caption = "清除分组";
                e.Menu.Items[3].Caption = "隐藏面板";
            }
            else if (e.MenuType == GridMenuType.Summary)
            {
                e.Menu.Items[0].Caption = "和";
                e.Menu.Items[1].Caption = "最小值";
                e.Menu.Items[2].Caption = "最大值";
                e.Menu.Items[3].Caption = "数量";
                e.Menu.Items[4].Caption = "平均值";
                e.Menu.Items[5].Caption = "无";
            }
        }

在WPF MVVM模式下,使用DevExpress GridControl实现鼠标右击获取单元格数据,可按以下步骤操作: 1. **在XAML中设置GridControl**: 在XAML文件里设置GridControl,同时添加鼠标右键点击事件绑定。借助`dxmvvm:Interaction.Triggers`和`dxmvvm:EventToCommand`把鼠标右键点击事件绑定到ViewModel里的命令。 ```xml <dxg:GridControl Grid.Column="1" x:Name="GridControl" ItemsSource="{Binding Persons}"> <dxmvvm:Interaction.Triggers> <dxmvvm:EventToCommand EventName="MouseRightButtonDown" Command="{Binding GridControlRightClickCmd}" CommandParameter="{Binding ElementName=GridControl}"/> </dxmvvm:Interaction.Triggers> <dxg:GridControl.View> <dxg:TableView ShowTotalSummary="True"/> </dxg:GridControl.View> </dxg:GridControl> ``` 2. **在ViewModel中实现命令**: 在ViewModel里实现`GridControlRightClickCmd`命令,在命令执行方法中获取鼠标右击的单元格数据。 ```csharp using DevExpress.Mvvm; using DevExpress.Xpf.Grid; using System.Windows.Input; public class ViewModel { public ICommand GridControlRightClickCmd { get; set; } public ViewModel() { GridControlRightClickCmd = new DelegateCommand<GridControl>(OnGridControlRightClick); } private void OnGridControlRightClick(GridControl gridControl) { if (gridControl != null) { var hitInfo = gridControl.CalcHitInfo(Mouse.GetPosition(gridControl)); if (hitInfo.InRowCell) { var row = hitInfo.Row; var column = hitInfo.Column; var cellValue = gridControl.GetCellValue(row, column); // 处理获取到的单元格数据 } } } } ``` 上述代码中,在XAML里把`GridControl`的`MouseRightButtonDown`事件绑定到ViewModel的`GridControlRightClickCmd`命令。在ViewModel的命令执行方法`OnGridControlRightClick`中,利用`CalcHitInfo`方法得到鼠标点击的单元格信息,进而获取单元格数据。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值