簡體   English   中英

如何禁用按鈕的 Enter 鍵

[英]How to disable Enter Key for a Button

我有一個按鈕,它綁定了一個命令,還有一個F1鍵的鍵綁定。 當焦點位於該按鈕上時, Enter鍵也會觸發我不想要的命令。 我希望該按鈕僅在F1和鼠標單擊時觸發。 如何禁用此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>

我的鍵綁定如下:

<UserControl.InputBindings>
    <KeyBinding Key="F1" Command="{Binding Path=TestViewModel.TestRunCommand}"/>
</UserControl.InputBindings>

當我移除焦點觸發器時, F1鍵也不起作用。 所以,我讓F1鍵工作的這個Setter屬性。

您可以為PreviewKeyDown事件附加事件處理程序:

<Button Name="TestButton" Content="Run (F1)" Margin="10,4" Width="100" FontSize="16" Command="{Binding TestRunCommand}" PreviewKeyDown="OnPreviewKeyDown">

在事件處理程序中,檢查Enter鍵並處理事件。

private void OnPreviewKeyDown(object sender, KeyEventArgs e)
{
   e.Handled = (e.Key == Key.Enter || e.Key == key.Return);
}

一個稍微更有創意的變體是創建一個不做任何事情的ICommand 它將作為KeyBinding綁定到Enter鍵,從而阻止原始命令。 我創建了一個標記擴展只是為了便於使用。

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>

只需從<Button...>中刪除Command="{Binding TestRunCommand}" ,按鈕在被按下時不會執行任何操作。

這是一個最小且完整的示例:

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

以及背后的代碼:

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

我終於想出了一個簡單的方法來通過添加KeyboardNavigation.AcceptsReturn="False"來阻止 Enter 鍵。

<Button Name="TestButton" Content="Run (F1)" Margin="10,4" Width="100" FontSize="16" Command="{Binding TestRunCommand}" KeyboardNavigation.AcceptsReturn="False">

這行得通。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM