如何限制文本框中的焦点

时间:2012-12-07 09:38:17

标签: wpf c#-4.0 wpf-controls

我有一个控件很少的对话框。有TextBox名为 txtControl 和两个Buttons 接受取消。我希望一旦焦点在 txtControl 中,焦点就不会消失,直到我点击接受取消按钮。

如果我尝试点击任何其他控件而不点击接受取消按钮,则焦点应保留在 txtControl 中。此外,我不想禁用或灰显其他控件。

4 个答案:

答案 0 :(得分:1)

你可以在root中处理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;
          }
      }
}

并且您可能还有预览KeyKeyDown以查看Tab是否被按下。

答案 1 :(得分:0)

这种限制不是一个好主意。

无法使用鼠标并使用Tab键在控件之间移动的人如何使用您的应用程序?

答案 2 :(得分:0)

您可以在根级别处理PreviewLostKeyboardFocus

在xaml

<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;
    }

答案 3 :(得分:0)

我会创建一个附加属性,查找文本框丢失Keyboad焦点,然后再次强制焦点重新回到文本框中。

附属物将是这样的。

  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>