界面快捷鍵資源
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