wpf menu 菜单 快捷键

界面快捷键资源

Ctrl+F F3可加入其它,自行定义

Page可改为Windows

xaml文件

<Style TargetType="{x:Type DataGrid}">

<Setter Property="ContextMenu">

<Setter.Value>

<ContextMenu StaysOpen="true">

<MenuItem Header="Copy" Command="{x:Static ApplicationCommands.Copy}">

</MenuItem>

</ContextMenu>

</Setter.Value>

</Setter>

</Style>

<Page.Resources>

<RoutedUICommand x:Key="F3" Text="查找内容"/>

<RoutedUICommand x:Key="Search" Text="查找内容"/>

</Page.Resources>

<Page.InputBindings>

<KeyBinding Gesture="F3" Command="{StaticResource Search}" />

<KeyBinding Gesture="Ctrl+F" Command="{StaticResource Search}"/>

</Page.InputBindings>

<Page.CommandBindings>

<CommandBinding Command="{StaticResource Search}" Executed="CommandBindingSearch_Executed"/>

<CommandBinding Command="{StaticResource F3}" Executed="CommandBindingSearch_Executed"/>

</Page.CommandBindings>

cs文件

private void CommandBindingSearch_Executed(object sender, ExecutedRoutedEventArgs e)

{

//执行查找

}

wpf button左键菜单

ContextMenu cmnu = new ContextMenu();

cmnu.Style = null;//可从字典读取

MenuItem item = new MenuItem() { Name = "mnuOpen", Header = "Open" };

item.Click += MenuItem_Click;

cmnu.Items.Add(item);

item = new MenuItem() { Name = "mnuClose", Header = "Close" };

item.Click += MenuItem_Click;

cmnu.Items.Add(item);

btn.Click += (bn, ev) =>

{

cmnu.Tag = btn.Tag;

cmnu.PlacementTarget = btn;

cmnu.Placement = System.Windows.Controls.Primitives.PlacementMode.Right;

cmnu.IsOpen = true;

//或者 //dg可以改为其它的控件或直接设置ContextMenu

//dg.ContextMenu.Tag = btn.Tag;

//dg.ContextMenu.PlacementTarget = btn;

//dg.ContextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Right;

//dg.ContextMenu.IsOpen = true;

};

btn.Initialized += (o1, e1) => { btn.ContextMenu = null; };

原文链接:https://blog.csdn.net/weixin_53370274/article/details/116518680

相关推荐
分布式存储与RustFS6 小时前
告别复杂配置:用Milvus、RustFS和Vibe Coding,60分钟DIY专属Chatbot
wpf·文件系统·milvus·对象存储·minio·rustfs·vibe
攻城狮CSU18 小时前
WPF 绑定机制实现原理
wpf
攻城狮CSU18 小时前
WPF 之数据绑定一(Data Binding)
wpf
wuty0071 天前
记录一下 WPF进程 SendMessage 发送窗口消息进行进程间通信,存在进程权限无法接受消息的问题
wpf·进程间通信·sendmessage·进程权限
c#上位机2 天前
wpf之ToggleButton控件
c#·wpf
浪扼飞舟2 天前
WPF用户控件和依赖属性
wpf
c#上位机3 天前
wpf之MVVM中只读属性更新界面
c#·wpf·mvvm
就是有点傻3 天前
WPF自定义控件之环形进度条
wpf
He BianGu3 天前
【笔记】WPF中如何的动态设置DataGridTextColumn是否显示
笔记·wpf
Pota-to成长日记3 天前
Redisson 看门狗机制深度解析:分布式锁的守护者
分布式·wpf