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

相关推荐
Chris _data2 天前
WPF 学习第三天 — Modbus RTU 串口通信
hadoop·学习·wpf
布吉岛的石头2 天前
Java 程序员第 43 阶段05:微服务整合大模型,跨服务调用架构设计实战,Seata分布式事务实战
wpf
步步为营DotNet2 天前
基于.NET Aspire 实现云原生应用的高效监控与可观测性
云原生·.net·wpf
芒鸽3 天前
HarmonyOS 分布式开发实战:设备协同、数据共享与跨设备迁移
分布式·wpf·harmonyos
Volunteer Technology3 天前
Flink状态管理与容错(二)
大数据·flink·wpf
happyprince3 天前
07_verl-Trainer模块详解
人工智能·架构·wpf·强化学习
bugcome_com4 天前
WPF + Prism 技术指南与实战项目(二、模板搭建)
wpf
小满Autumn4 天前
log4net 日志框架 — 从配置到实战速查手册
笔记·c#·.net·wpf·上位机·log4net
政沅同学4 天前
基于 C# WPF + HALCON 的工业视觉算法工具框架(开源)
开发语言·c#·wpf
happyprince5 天前
03_verl-设计理念与核心原理
wpf