我有一个带有少量控件的对话框。有一个TextBox
名为txtControl和两个Buttons
Accept和Cancel。我希望一旦焦点位于txtControl中,焦点就不会消失,直到我单击Accept或Cancel按钮。
如果我尝试单击任何其他控件而不单击Accept或Cancel按钮,则焦点应保留在txtControl中。此外,我不想禁用或灰显其他控件。
我有一个带有少量控件的对话框。有一个TextBox
名为txtControl和两个Buttons
Accept和Cancel。我希望一旦焦点位于txtControl中,焦点就不会消失,直到我单击Accept或Cancel按钮。
如果我尝试单击任何其他控件而不单击Accept或Cancel按钮,则焦点应保留在txtControl中。此外,我不想禁用或灰显其他控件。
您可以在根目录中处理 OnPreviewMouseDown,只要焦点位于 txtControl 上,并且鼠标不在 txtControl、Accept 或 Cancel 上方;
void mainWindow_previewMouseDown(object sender, MouseEventArg e)
{
if (txtControl.IsFocusWithin)
{
if (txtControl.IsMouseOver == false ||
accept.IsMouseOver ==false ||
cancel.IsMouseOver ==false)
{
e.Handle = true;
}
}
}
你也可以用 PreviewKeyDown 来查看 Tab 是否被按下。
这种限制不是一个好主意。
无法使用鼠标并使用 tab 键在控件之间移动的人将如何使用您的应用程序?
您可以在根级别处理PreviewLostKeyboardFocus 。
在xml中
<Window ... PreviewLostKeyboardFocus="Win_PreviewLostKeyboardFocus">
在 C# 中
private void Win_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
// change focus behavior only when txtControl
// is the element losing focus
if (e.OldFocus != txtControl)
return;
// if new element with focus is not Accept and is not Cancel, then disable the focus change
if (e.NewFocus != Accept && e.NewFocus != Cancel)
e.Handled = true;
}
我会创建一个附加属性来寻找失去键盘焦点的文本框,然后再次将焦点重新回到文本框。
附加属性将是这样的。
public class TextBoxExtras : DependencyObject
{
public static bool GetRetainsFocus(DependencyObject obj)
{
return (bool)obj.GetValue(RetainsFocusProperty);
}
public static void SetRetainsFocus(DependencyObject obj, bool value)
{
obj.SetValue(RetainsFocusProperty, value);
}
public static readonly DependencyProperty RetainsFocusProperty =
DependencyProperty.RegisterAttached("RetainsFocus", typeof(bool), typeof(TextBoxExtras), new PropertyMetadata(false, new PropertyChangedCallback((s, e) =>
{
TextBox textBox = s as TextBox;
if (textBox != null)
{
if (!(bool)e.NewValue && (bool)e.OldValue)
textBox.LostKeyboardFocus -= textBox_LostKeyboardFocus;
if ((bool)e.NewValue)
{
textBox.LostKeyboardFocus += textBox_LostKeyboardFocus;
textBox.Unloaded += textBox_Unloaded;
}
}
})));
static void textBox_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
TextBox textBox = sender as TextBox;
if (textBox != null )
if (textBox.Focusable)
textBox.Focus();
}
static void textBox_Unloaded(object sender, RoutedEventArgs e)
{
TextBox textBox = sender as TextBox;
if (textBox != null)
{
textBox.LostKeyboardFocus -= textBox_LostKeyboardFocus;
textBox.Unloaded -= textBox_Unloaded;
}
}
}
并像这样在 XAML 中使用它,第一个文本框是“保持焦点”的文本框
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication4"
Background="Black"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox local:TextBoxExtras.RetainsFocus="True" Margin="10,10,387,283"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="10,37,0,260" Width="120" />
<Button Content="Accept" HorizontalAlignment="Left" Margin="10,81,0,0" VerticalAlignment="Top" Width="75" />
</Grid>
</Window>