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

相关推荐
Scout-leaf8 天前
WPF新手村教程(三)—— 路由事件
c#·wpf
柒.梧.10 天前
基于SpringBoot+JWT 实现Token登录认证与登录人信息查询
wpf
十月南城13 天前
Flink实时计算心智模型——流、窗口、水位线、状态与Checkpoint的协作
大数据·flink·wpf
听麟16 天前
HarmonyOS 6.0+ 跨端会议助手APP开发实战:多设备接续与智能纪要全流程落地
分布式·深度学习·华为·区块链·wpf·harmonyos
@hdd16 天前
Kubernetes 可观测性:Prometheus 监控、日志采集与告警
云原生·kubernetes·wpf·prometheus
zls36536516 天前
C# WPF canvas中绘制缺陷分布map
开发语言·c#·wpf
专注VB编程开发20年16 天前
c#Redis扣款锁的设计,多用户,多台电脑操作
wpf
闲人编程17 天前
定时任务与周期性调度
分布式·python·wpf·调度·cron·定时人物·周期性
zls36536517 天前
C# WPF canvas中绘制缺陷分布map并实现缩放
开发语言·c#·wpf
数据知道18 天前
PostgreSQL:Citus 分布式拓展,水平分片,支持海量数据与高并发
分布式·postgresql·wpf