[英]How to disable Enter Key for a Button
I have a button that has a command binded to it and also a key binding for F1 key.我有一个按钮,它绑定了一个命令,还有一个F1键的键绑定。 When the focus is on that button, the Enter key also triggers the command, which I do not want.
当焦点位于该按钮上时, Enter键也会触发我不想要的命令。 I want the button to be triggered only for F1 and mouse click.
我希望该按钮仅在F1和鼠标单击时触发。 How to disable this Enter key?
如何禁用此Enter键?
<Button Name="TestButton" Content="Run (F1)" Margin="10,4" Width="100" FontSize="16" Command="{Binding TestRunCommand}">
<Button.Style>
<Style TargetType="Button" BasedOn="{StaticResource ResourceKey=BlackButton}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsEnabled,ElementName=TestButton}" Value="True">
<Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=TestButton}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
My key binding is as follows:我的键绑定如下:
<UserControl.InputBindings>
<KeyBinding Key="F1" Command="{Binding Path=TestViewModel.TestRunCommand}"/>
</UserControl.InputBindings>
When I remove the focus trigger, then the F1 key is also not working.当我移除焦点触发器时, F1键也不起作用。 So, I had this
Setter
property for F1 key to work.所以,我让F1键工作的这个
Setter
属性。
You can attach an event handler for the PreviewKeyDown
event:您可以为
PreviewKeyDown
事件附加事件处理程序:
<Button Name="TestButton" Content="Run (F1)" Margin="10,4" Width="100" FontSize="16" Command="{Binding TestRunCommand}" PreviewKeyDown="OnPreviewKeyDown">
In the event handler, check for the Enter key and handle the event.在事件处理程序中,检查Enter键并处理事件。
private void OnPreviewKeyDown(object sender, KeyEventArgs e)
{
e.Handled = (e.Key == Key.Enter || e.Key == key.Return);
}
A slightly more creative variant would be to create an ICommand
that does not do anything.一个稍微更有创意的变体是创建一个不做任何事情的
ICommand
。 It would be bound as KeyBinding
to the Enter key, thus blocking the original command.它将作为
KeyBinding
绑定到Enter键,从而阻止原始命令。 I created a markup extension just for the ease of use.我创建了一个标记扩展只是为了便于使用。
public class IgnoreCommandExtension : MarkupExtension
{
private static readonly IgnoreCommand IgnoreCommandInstance = new IgnoreCommand();
private class IgnoreCommand : ICommand
{
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
}
public event EventHandler CanExecuteChanged;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return IgnoreCommandInstance;
}
}
<Button Name="TestButton" Content="Run (F1)" Margin="10,4" Width="100" FontSize="16" Command="{Binding TestRunCommand}">
<Button.InputBindings>
<KeyBinding Key="Enter" Command="{local:IgnoreCommand}"/>
<KeyBinding Key="Return" Command="{local:IgnoreCommand}"/>
</Button.InputBindings>
<!-- ...other code. -->
</Button>
Just remove Command="{Binding TestRunCommand}"
from <Button...>
and the button won't do anything when being pressed.只需从
<Button...>
中删除Command="{Binding TestRunCommand}"
,按钮在被按下时不会执行任何操作。
Here's a minimal and complete example:这是一个最小且完整的示例:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<local:DC/>
</Window.DataContext>
<Window.InputBindings>
<KeyBinding Key="F1" Command="{Binding Path=F1Command}"/>
</Window.InputBindings>
<StackPanel>
<Button Name="TestButton" Content="Run (F1)" Margin="10,4" Width="100" FontSize="16"
Command="{Binding TestRunCommand}">
<!-- ^- remove this if you want it to do nothing -->
<Button.Style>
<Style TargetType="Button" >
<Style.Triggers>
<DataTrigger Binding="{Binding IsEnabled,ElementName=TestButton}" Value="True">
<Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=TestButton}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</StackPanel>
</Window>
And the code behind:以及背后的代码:
public class DC
{
public ICommand TestRunCommand
{
get { return new CommandHandler(() => MessageBox.Show("Enter / Space / Click"), () => true); }
}
public ICommand F1Command
{
get { return new CommandHandler(() => MessageBox.Show("F1"), () => true); }
}
}
public class CommandHandler : ICommand
{
private readonly Action _action;
private readonly Func<bool> _canExecute;
public CommandHandler(Action action, Func<bool> canExecute)
{
_action = action;
_canExecute = canExecute;
}
public event EventHandler CanExecuteChanged
{
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}
public bool CanExecute(object parameter) => _canExecute.Invoke();
public void Execute(object parameter) => _action();
}
I finally figured out a simple way to block Enter Key by adding KeyboardNavigation.AcceptsReturn="False"
.我终于想出了一个简单的方法来通过添加
KeyboardNavigation.AcceptsReturn="False"
来阻止 Enter 键。
<Button Name="TestButton" Content="Run (F1)" Margin="10,4" Width="100" FontSize="16" Command="{Binding TestRunCommand}" KeyboardNavigation.AcceptsReturn="False">
This worked.这行得通。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.