WPF 与 WinForm 调用系统右键菜单/资源管理器右键菜单/桌面右键菜单

原本此类是 http://www.codeproject.com 上高手写的 ShellContextMenu 类

但是该类原本貌似是为 WinForm 设计的 ,  默认的命名空间和引用也是 WinForm 的

但是最近有需求要让 WPF 也能使用该类所实现的功能 !  然后实践中发现 WPF 也能使用, 不过就是要在 WPF 中引用该类用到的 WinForm 命名空间就行了

具体的请看 示例 Demo 下载: http://download.csdn.net/detail/herluckily/9578026


        /// <summary>
        /// ShellContextMenu 文件夹右键菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            ShellContextMenu scm = new ShellContextMenu();

            string fileName = @"D:\Test\测试文件夹";

            DirectoryInfo[] folders = new DirectoryInfo[1];

            folders[0] = new DirectoryInfo(fileName);

            scm.ShowContextMenu(folders, System.Windows.Forms.Cursor.Position);
        }

        /// <summary>
        /// ShellContextMenu 文件右键菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Peter.ShellContextMenu scm = new Peter.ShellContextMenu();

            FileInfo[] files = new FileInfo[1];

            string fileName = @"D:\Test\测试文本文档.txt";

            files[0] = new FileInfo(fileName);

            scm.ShowContextMenu(files, System.Windows.Forms.Cursor.Position);
        }







示例 Demo 下载: http://download.csdn.net/detail/herluckily/9578026

WPF中,可以通过以下步骤为DataGrid增加右键菜单: 1. 首先,在XAML文件中定义一个ContextMenu,用于作为右键菜单的内容。可以在Window或者UserControl的资源部分定义ContextMenu,例如: ```xaml <Window.Resources> <ContextMenu x:Key="DataGridContextMenu"> <MenuItem Header="编辑" Click="EditMenuItem_Click"/> <MenuItem Header="删除" Click="DeleteMenuItem_Click"/> </ContextMenu> </Window.Resources> ``` 2. 然后,在DataGrid的样式中,使用EventSetter来为DataGrid的PreviewMouseRightButtonDown事件添加处理程序。在处理程序中,将ContextMenu设置为右键菜单,并设置菜单的PlacementTarget为DataGrid,以确保菜单与DataGrid关联。例如: ```xaml <DataGrid> <DataGrid.RowStyle> <Style TargetType="DataGridRow"> <EventSetter Event="PreviewMouseRightButtonDown" Handler="DataGridRow_PreviewMouseRightButtonDown"/> </Style> </DataGrid.RowStyle> </DataGrid> ``` 3. 最后,在代码文件中实现事件处理程序,将ContextMenu设置为右键菜单,并将菜单的PlacementTarget设置为DataGrid。例如: ```csharp private void DataGridRow_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e) { DataGridRow row = sender as DataGridRow; if (row != null) { DataGrid dataGrid = FindVisualParent<DataGrid>(row); if (dataGrid != null) { ContextMenu contextMenu = dataGrid.Resources["DataGridContextMenu"] as ContextMenu; if (contextMenu != null) { contextMenu.PlacementTarget = dataGrid; contextMenu.IsOpen = true; e.Handled = true; } } } } private static T FindVisualParent<T>(UIElement element) where T : UIElement { UIElement parent = element; while (parent != null) { T foundElement = parent as T; if (foundElement != null) { return foundElement; } parent = VisualTreeHelper.GetParent(parent) as UIElement; } return null; } ``` 这样,当在DataGrid的行上右键点击时,就会显示出定义的右键菜单
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lovevalesail

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值