トップ回答者
KeyBindingでコマンドを読んだとき、UpdateSourceTrigger=LostFocusが呼ばれない

質問
-
お世話になります。
Viewにバインドされたデータの更新・取得についてお教えください。
WindowのKeyBindingとボタンに割り付けられたコマンドは同一の処理を呼んでいます。
あるテキストボックスのUpdateSourceTriggerがLostFocusに設定されていた場合、
テキストボックスの値を変更して、そのままコマンドを実行した場合・・・
1)マウスでボタンを押下してコマンドを呼んだ。
2)キーボードのボタンを押してコマンドを呼んだ
1)の場合は、LostFocusが正しく発生しコマンド内でそのデータが取得できます。
2)の場合はフォーカスが遷移しないため、データが正しく取得できません。
2)の時にも、LostFocusを動かしてデータを更新させたいのですが・・・
よろしくお願いします。https://dl.dropboxusercontent.com/u/14501064/test18.zip
MainWindow
<Window> <Window.InputBindings> <KeyBinding Key="F1" Command="{Binding F1Command}" /> <KeyBinding Key="F2" Command="{Binding F2Command}" /> </Window.InputBindings> <StackPanel> <TextBox Text="{Binding Txt1,UpdateSourceTrigger=LostFocus}" Width="200" ></TextBox> <Button Content="btn F1" Command="{Binding F1Command}" Width="100" HorizontalAlignment="Left"></Button> </StackPanel> </Window>
- 編集済み r1user 2013年9月2日 5:12
回答
-
Azuleanさん、
回答ありがとうございます。
UpdateSource を呼ぶ方向で考えていましたが、なにぶん画面数&部品が多く、ご指摘の通り手間がかかりそうなので悩んでおりました。FocusManagerとか何かの使い方でさくっとできないかなと思いまして・・・
Focusを移動させればよいので、今回はLostFocusを無理やり発生させる方向で行こうと思っています。
フォーカスの取得をするビヘイビアを作成し、画面端にOpacity="0"のテキストボックスを配置し、そちらにフォーカスを強制移動する方向を考えています。ありがとうございました。
// フォーカス取得ビヘイビア
}
public static readonly DependencyProperty Focusing = DependencyProperty.RegisterAttached("Focusing", typeof(bool), typeof(myBehaviors), new UIPropertyMetadata(false, FocusingChanged)); public static bool GetFocusing(DependencyObject obj) { return (bool)obj.GetValue(Focusing); } public static void SetFocusing(DependencyObject obj, bool value) { obj.SetValue(Focusing, value); } public static void FocusingChanged(DependencyObject sender, DependencyPropertyChangedEventArgs evt) { UIElement element = (UIElement)sender; if (evt.NewValue.Equals(true)) { element.Focus();
}
- 回答としてマーク r1user 2013年9月2日 23:48
すべての返信
-
Azuleanさん、
回答ありがとうございます。
UpdateSource を呼ぶ方向で考えていましたが、なにぶん画面数&部品が多く、ご指摘の通り手間がかかりそうなので悩んでおりました。FocusManagerとか何かの使い方でさくっとできないかなと思いまして・・・
Focusを移動させればよいので、今回はLostFocusを無理やり発生させる方向で行こうと思っています。
フォーカスの取得をするビヘイビアを作成し、画面端にOpacity="0"のテキストボックスを配置し、そちらにフォーカスを強制移動する方向を考えています。ありがとうございました。
// フォーカス取得ビヘイビア
}
public static readonly DependencyProperty Focusing = DependencyProperty.RegisterAttached("Focusing", typeof(bool), typeof(myBehaviors), new UIPropertyMetadata(false, FocusingChanged)); public static bool GetFocusing(DependencyObject obj) { return (bool)obj.GetValue(Focusing); } public static void SetFocusing(DependencyObject obj, bool value) { obj.SetValue(Focusing, value); } public static void FocusingChanged(DependencyObject sender, DependencyPropertyChangedEventArgs evt) { UIElement element = (UIElement)sender; if (evt.NewValue.Equals(true)) { element.Focus();
}
- 回答としてマーク r1user 2013年9月2日 23:48