ボタンの動作を変更しようとしています。そのためにはコードを使用することをお勧めします。最も簡単な方法は、次のようにプレビュー イベントをウィンドウにアタッチすることです。
<Window
...
PreviewKeyDown="HandlePreviewKeyDown">
次に、コードで次のように処理します。
private void HandlePreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.IsRepeat)
{
e.Handled = true;
}
}
悲しいことに、フォームによってホストされているテキストボックスであっても、これにより繰り返し動作が無効になります。これは興味深い質問です。これを行うためのよりエレガントな方法を見つけたら、答えに追加します。
編集:
OK、キー バインドを定義するには 2 つの方法があります。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.InputBindings>
<KeyBinding x:Name="altD" Gesture="Alt+D" Command="{Binding ClickCommand}"/>
</Window.InputBindings>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Content="_Click" Command="{Binding ClickCommand}" />
<TextBox Grid.Row="1"/>
</Grid>
</Window>
_Click
アンダースコア:コンテンツを介して暗黙的に Alt-C ジェスチャを要求したため、上記のボタンはクリックを生成します。次に、ウィンドウには Alt+D への明示的なキーバインドがあります。
このコード ビハインドは両方のケースで機能するようになり、通常の繰り返しに干渉することはありません。
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
base.OnPreviewKeyDown(e);
if (e.IsRepeat)
{
if (((KeyGesture)altD.Gesture).Matches(this, e))
{
e.Handled = true;
}
else if (e.Key == Key.System)
{
string sysKey = e.SystemKey.ToString();
//We only care about a single character here: _{character}
if (sysKey.Length == 1 && AccessKeyManager.IsKeyRegistered(null, sysKey))
{
e.Handled = true;
}
}
}
}