質問は非常に単純です:既にフォーカスされているコントロールでLostFocus
発生するのを防ぐにはどうすればよいですか?MouseDown
次のコントロールがあります (すべてのイベント バインディングを使用した一時的なテスト)。
<Grid Name="gBase" Focusable="True" MouseUp="SetFocus" MouseDown="SetFocus" MouseMove="gBase_MouseMove" PreviewDragEnter="gBase_PreviewDragEnter" LostFocus="gBase_LostFocus" GotFocus="gBase_GotFocus" Background="DarkRed" Width="500" Height="250" />
そして、次のコードビハインド:
private void SetFocus(object sender, MouseButtonEventArgs e)
{
Grid g = sender as Grid;
g.Focus();
}
private void gBase_LostFocus(object sender, RoutedEventArgs e)
{
Grid g = sender as Grid;
g.Background = Brushes.DarkRed;
}
private void gBase_GotFocus(object sender, RoutedEventArgs e)
{
Grid g = sender as Grid;
g.Background = Brushes.Aquamarine;
}
private void gBase_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
Grid g = sender as Grid;
g.Focus();
}
}
private void gBase_PreviewDragEnter(object sender, DragEventArgs e)
{
Grid g = sender as Grid;
g.Focus();
}
グリッドをクリックすると、フォーカスが得られます。
問題は、グリッドが既にフォーカスを取得している場合、マウス ボタンを押している間にフォーカスが失われ、離すか移動するまで元に戻らないことです。私が好む動作は、そもそもフォーカスが失われないようにすることです。