gpt4 book ai didi

wpf - 在 Wpf 应用程序中实现快捷键

转载 作者:行者123 更新时间:2023-12-04 18:20:26 26 4
gpt4 key购买 nike

我是 wpf 应用程序的新手,我正在开发应用程序,我已经创建了一个菜单现在我想在快捷键 ctrl+o、ctrl+n 等上运行菜单项事件。我该怎么做。请给我详细信息。

最佳答案

您可以通过以下方式实现......

在 Xaml 文件中

<Window x:Class="FocusDemo.Window1"  
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:FocusDemo"
Title="Window1" Height="300" Width="300">

<Window.CommandBindings>
<CommandBinding
Command=
"{x:Static
local:Window1.CustomRoutedCommand}"
Executed="ExecutedCustomCommand"
CanExecute="CanExecuteCustomCommand" >
</CommandBinding>
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding
Command=
"{x:Static
local:Window1.CustomRoutedCommand}"
Key="S"
Modifiers="Control"/>
</Window.InputBindings>
<Grid>
<!--Your Controls-->
</Grid>
</Window>

在代码隐藏文件中

    /// <summary>  
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public static RoutedCommand CustomRoutedCommand = new RoutedCommand();
public Window1()
{
InitializeComponent();
}
#region
public void ExecutedCustomCommand(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Ctrl+S");
}


public void CanExecuteCustomCommand(object sender,
CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
#endregion

}

来源:Click here

如果正确请不要忘记标记答案

关于wpf - 在 Wpf 应用程序中实现快捷键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8060893/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com