簡體   English   中英

如何在文本框中限制焦點

[英]How to restrict focus in textbox

我有一個帶有少量控件的對話框。 有一個名為txtControlTextBox和兩個Buttons AcceptCancel 我希望一旦焦點位於txtControl中 ,焦點就不會消失,直到我單擊“ 接受”或“ 取消”按鈕。

如果我嘗試單擊任何其他控件而不單擊“ 接受”或“ 取消”按鈕,則焦點應保留在txtControl中 另外我也不想禁用或禁用其他控件。

每當焦點位於txtControl上並且鼠標不在txtControl上方時,您都可以在根目錄中處理OnPreviewMouseDown;請單擊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

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

我將創建一個附加屬性,該屬性用於查找失去鍵盤提示焦點的文本框,然后再次將焦點重新強制回到文本框。

附加的屬性將是這樣的。

  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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM