none
TextBox里面的内容如何全选 RRS feed

  • 问题

  • 我想问一下,如果我的鼠标进入TextBox时,TextBox里面所有的字处在一个全选的状态,这个全选的效果怎么实现?谢谢。我自己试了一下,用SelectedText属性,貌似没什么效果。
    2011年4月21日 4:08

答案

  •   public class TextBoxHelper
      {
        public static readonly DependencyProperty AutoSelectAllProperty =
          DependencyProperty.RegisterAttached("AutoSelectAll", typeof(bool), typeof(TextBoxHelper),
            new FrameworkPropertyMetadata((bool)false,
              new PropertyChangedCallback(OnAutoSelectAllChanged)));
    
        public static bool GetAutoSelectAll(TextBoxBase d)
        {
          return (bool)d.GetValue(AutoSelectAllProperty);
        }
    
        public static void SetAutoSelectAll(TextBoxBase d, bool value)
        {
          d.SetValue(AutoSelectAllProperty, value);
        }
    
        private static void OnAutoSelectAllChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
          var textBox = d as TextBoxBase;
          if (textBox != null)
          {
            var flag = (bool)e.NewValue;
            if (flag)
            {
              textBox.GotFocus += TextBoxOnGotFocus;
            }
            else
            {
              textBox.GotFocus -= TextBoxOnGotFocus;
            }
          }
        }
    
        private static void TextBoxOnGotFocus(object sender, RoutedEventArgs e)
        {
          var textBox = sender as TextBoxBase;
          if (textBox != null)
          {
            textBox.SelectAll();
          }
        }
      }
    
    
    
    然后在Style里面
    <Setter Property="local:TextBoxHelper.AutoSelectAll" Value="True"/>
    
    
    
    2011年4月21日 5:25
  • 你可以处理MouseEnter event, 然后:

        TextBox.Focus();
        TextBox.SelectAll();
        e.Handled = true;

     

     

     


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年4月25日 8:50
    版主

全部回复

  •   public class TextBoxHelper
      {
        public static readonly DependencyProperty AutoSelectAllProperty =
          DependencyProperty.RegisterAttached("AutoSelectAll", typeof(bool), typeof(TextBoxHelper),
            new FrameworkPropertyMetadata((bool)false,
              new PropertyChangedCallback(OnAutoSelectAllChanged)));
    
        public static bool GetAutoSelectAll(TextBoxBase d)
        {
          return (bool)d.GetValue(AutoSelectAllProperty);
        }
    
        public static void SetAutoSelectAll(TextBoxBase d, bool value)
        {
          d.SetValue(AutoSelectAllProperty, value);
        }
    
        private static void OnAutoSelectAllChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
          var textBox = d as TextBoxBase;
          if (textBox != null)
          {
            var flag = (bool)e.NewValue;
            if (flag)
            {
              textBox.GotFocus += TextBoxOnGotFocus;
            }
            else
            {
              textBox.GotFocus -= TextBoxOnGotFocus;
            }
          }
        }
    
        private static void TextBoxOnGotFocus(object sender, RoutedEventArgs e)
        {
          var textBox = sender as TextBoxBase;
          if (textBox != null)
          {
            textBox.SelectAll();
          }
        }
      }
    
    
    
    然后在Style里面
    <Setter Property="local:TextBoxHelper.AutoSelectAll" Value="True"/>
    
    
    
    2011年4月21日 5:25
  • 你可以处理MouseEnter event, 然后:

        TextBox.Focus();
        TextBox.SelectAll();
        e.Handled = true;

     

     

     


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年4月25日 8:50
    版主