我有一個按鈕,它系結了一個命令,還有一個鍵系結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鍵也不起作用。所以,我有這個Setter屬性作為F1作業的關鍵。
uj5u.com熱心網友回復:
您可以為該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>
uj5u.com熱心網友回復:
只需洗掉Command="{Binding TestRunCommand}",<Button ...>按鈕在按下時不會執行任何操作。
這是一個最小且完整的示例:
<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();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/376972.html
