Winforms TextBoxのKeyDownハンドラーで(ShowDialogを使用して)WPFウィンドウを起動しています。WPFウィンドウには、IsDefault=trueのボタンがあります。テキストボックスでEnterキーを押した後、WPF AccessKeyメカニズムは、ウィンドウの読み込み後にそれを取得し、デフォルトのボタンをトリガーするようです。
この問題は、WPFTextBoxのKeyDownハンドラーからウィンドウを起動する場合には発生しません。
Winformsテキストボックスで処理されているキーの押下をWPFウィンドウが受け取らないようにする回避策を探しています。
問題を説明するサンプルコードを次に示します。
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
var window = new System.Windows.Window();
var button = new System.Windows.Controls.Button
{
IsDefault = true,
Content = "OK",
};
button.Click += (s, args) => window.Close();
window.Content = button;
// window loads then immediately closes due to the default button being triggered
window.ShowDialog();
}
}