トップ回答者
コントロールのIsPressed状態をEnterキーでも行う方法

質問
回答
-
めんどくさいので継承でやってみる
class ButtonEx : Button { protected override void OnKeyDown(System.Windows.Input.KeyEventArgs e) { base.OnKeyDown(e); if (e.Key == System.Windows.Input.Key.Enter && IsFocused) { this.IsPressed=true; } } protected override void OnKeyUp(System.Windows.Input.KeyEventArgs e) { base.OnKeyDown(e); if (e.Key == System.Windows.Input.Key.Enter && IsFocused) { this.IsPressed = false; } } } class CheckBoxEx : CheckBox { protected override void OnKeyDown(System.Windows.Input.KeyEventArgs e) { base.OnKeyDown(e); if (e.Key == System.Windows.Input.Key.Enter && IsFocused) { this.IsPressed = true; } } protected override void OnKeyUp(System.Windows.Input.KeyEventArgs e) { base.OnKeyDown(e); if (e.Key == System.Windows.Input.Key.Enter && IsFocused) { this.IsPressed = false; } } } class RadioButtonEx : RadioButton { protected override void OnKeyDown(System.Windows.Input.KeyEventArgs e) { base.OnKeyDown(e); if (e.Key == System.Windows.Input.Key.Enter && IsFocused) { this.IsPressed = true; } } protected override void OnKeyUp(System.Windows.Input.KeyEventArgs e) { base.OnKeyDown(e); if (e.Key == System.Windows.Input.Key.Enter && IsFocused) { this.IsPressed = false; } } }
個別に明示されていない限りgekkaがフォーラムに投稿したコードにはフォーラム使用条件に基づき「MICROSOFT LIMITED PUBLIC LICENSE」が適用されます。(かなり自由に使ってOK!)
- 回答としてマーク しん11111111 2016年3月12日 10:09
-
gekkaさんのコードを参考に以下の方法で実現できました。
public class ButtonEx : Button { protected override void OnKeyDown(KeyEventArgs e) { if (e.Key == Key.Enter && IsFocused) { this.IsPressed = true; return; } base.OnKeyDown(e); } protected override void OnKeyUp(KeyEventArgs e) { if (e.Key == Key.Enter && IsFocused) { this.IsPressed = false; base.OnKeyDown(e); return; } base.OnKeyUp(e); } }
- 回答としてマーク しん11111111 2016年3月12日 12:00
すべての返信
-
こんにちは。
スタイルの適用のみであれば以下のような方法でVisualStateManagerを使って制御する必要がありそうです。
WPF Pressing Enter Key How to Make Button look pressed
動作を含めた話であれば、キーのコンバートを行ったほうが楽そうですが。
-
スタイル、動作ともにスペースキーと同じように行いたいと思っています。
private void button_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e) { if (e.Key == System.Windows.Input.Key.Enter) { // 読み取り専用のため設定できないが // 以下のようにEnterキーのイベントをスペースキーが押されたように振るまうイメージ //e.Key = System.Windows.Input.Key.Space; } }
>動作を含めた話であれば、キーのコンバートを行ったほうが楽そうですが
上記のような方法があるのでしょうか?
-
めんどくさいので継承でやってみる
class ButtonEx : Button { protected override void OnKeyDown(System.Windows.Input.KeyEventArgs e) { base.OnKeyDown(e); if (e.Key == System.Windows.Input.Key.Enter && IsFocused) { this.IsPressed=true; } } protected override void OnKeyUp(System.Windows.Input.KeyEventArgs e) { base.OnKeyDown(e); if (e.Key == System.Windows.Input.Key.Enter && IsFocused) { this.IsPressed = false; } } } class CheckBoxEx : CheckBox { protected override void OnKeyDown(System.Windows.Input.KeyEventArgs e) { base.OnKeyDown(e); if (e.Key == System.Windows.Input.Key.Enter && IsFocused) { this.IsPressed = true; } } protected override void OnKeyUp(System.Windows.Input.KeyEventArgs e) { base.OnKeyDown(e); if (e.Key == System.Windows.Input.Key.Enter && IsFocused) { this.IsPressed = false; } } } class RadioButtonEx : RadioButton { protected override void OnKeyDown(System.Windows.Input.KeyEventArgs e) { base.OnKeyDown(e); if (e.Key == System.Windows.Input.Key.Enter && IsFocused) { this.IsPressed = true; } } protected override void OnKeyUp(System.Windows.Input.KeyEventArgs e) { base.OnKeyDown(e); if (e.Key == System.Windows.Input.Key.Enter && IsFocused) { this.IsPressed = false; } } }
個別に明示されていない限りgekkaがフォーラムに投稿したコードにはフォーラム使用条件に基づき「MICROSOFT LIMITED PUBLIC LICENSE」が適用されます。(かなり自由に使ってOK!)
- 回答としてマーク しん11111111 2016年3月12日 10:09
-
gekkaさんのコードを参考に以下の方法で実現できました。
public class ButtonEx : Button { protected override void OnKeyDown(KeyEventArgs e) { if (e.Key == Key.Enter && IsFocused) { this.IsPressed = true; return; } base.OnKeyDown(e); } protected override void OnKeyUp(KeyEventArgs e) { if (e.Key == Key.Enter && IsFocused) { this.IsPressed = false; base.OnKeyDown(e); return; } base.OnKeyUp(e); } }
- 回答としてマーク しん11111111 2016年3月12日 12:00