当前位置:   article > 正文

wpf menu 菜单 快捷键_wpf menu 快捷键

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)
{
    //执行查找
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

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; };

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/493395
推荐阅读
相关标签