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

相关推荐
军训猫猫头43 分钟前
20.抽卡只有金,带保底(WPF) C#
ui·c#·wpf
明耀1 小时前
WPF 设置平均布局 如果隐藏的话,能够自动扩展
wpf
晚安苏州14 小时前
WPF DataTemplate 数据模板
wpf
甜甜不吃芥末1 天前
WPF依赖属性详解
wpf
Hat_man_2 天前
WPF制作图片闪烁的自定义控件
wpf
晚安苏州3 天前
WPF Binding 绑定
wpf·wpf binding·wpf 绑定
wangnaisheng3 天前
【WPF】RenderTargetBitmap的使用
wpf
dotent·4 天前
WPF 完美解决改变指示灯的颜色
wpf
orangapple5 天前
WPF 用Vlc.DotNet.Wpf实现视频播放、停止、暂停功能
wpf·音视频
ysdysyn5 天前
wpf mvvm 数据绑定数据(按钮文字表头都可以),根据长度进行换行,并把换行的文字居中
c#·wpf·mvvm