ここでコンセプトに関する質問があります。TextBoxまたは 内のすべてのテキストを選択する方法を知っていますPasswordBox。経由GotKeyboardFocusとPreviewMouseLeftButtonDownイベント、ご存知のとおりです。これはうまくいきます。
XAML:
PreviewMouseLeftButtonDown="PasswordOnPreviewMouseDown"
GotKeyboardFocus="SelectAllPassword"
コードビハインド
private void SelectAllPassword(Object sender, RoutedEventArgs e)
{
var pb = (sender as PasswordBox);
if (pb != null)
pb.SelectAll();
}
private void PasswordOnPreviewMouseDown(Object sender, MouseButtonEventArgs e)
{
var pb = (sender as PasswordBox);
if (pb != null)
if (!pb.IsKeyboardFocusWithin)
{
e.Handled = true;
pb.Focus();
}
}
しかし問題は、なぜこれがうまくいかないのかということです。
XAML:
PreviewMouseLeftButtonDown="PasswordOnPreviewMouseDown"
分離コード:
private void PasswordOnPreviewMouseDown(Object sender, MouseButtonEventArgs e)
{
_txtPassword.SelectAll();
e.Handled = true;
}
どこで_txtPassword-TextBoxまたはPasswordBox制御します。では、なぜFocusテキスト コントロールを強制されているのでしょうか。