zoukankan      html  css  js  c++  java
  • wpf Textbox 点击选中全部文本

    用法:依赖属性
    SelectTextOnFocus.Active = True
    public class SelectTextOnFocus : DependencyObject
        {
            public static readonly DependencyProperty ActiveProperty = DependencyProperty.RegisterAttached(
                "Active",
                typeof(bool),
                typeof(SelectTextOnFocus),
                new PropertyMetadata(false, ActivePropertyChanged));
    
            private static void ActivePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                if (d is TextBox)
                {
                    TextBox textBox = d as TextBox;
                    if ((e.NewValue as bool?).GetValueOrDefault(false))
                    {
                        textBox.GotKeyboardFocus += OnKeyboardFocusSelectText;
                        textBox.PreviewMouseLeftButtonDown += OnMouseLeftButtonDown;
                    }
                    else
                    {
                        textBox.GotKeyboardFocus -= OnKeyboardFocusSelectText;
                        textBox.PreviewMouseLeftButtonDown -= OnMouseLeftButtonDown;
                    }
                }
            }
    
            private static void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                DependencyObject dependencyObject = GetParentFromVisualTree(e.OriginalSource);
    
                if (dependencyObject == null)
                {
                    return;
                }
    
                var textBox = (TextBox)dependencyObject;
                if (!textBox.IsKeyboardFocusWithin)
                {
                    textBox.Focus();
                    e.Handled = true;
                }
            }
    
            private static DependencyObject GetParentFromVisualTree(object source)
            {
                DependencyObject parent = source as UIElement;
                while (parent != null && !(parent is TextBox))
                {
                    parent = VisualTreeHelper.GetParent(parent);
                }
    
                return parent;
            }
    
            private static void OnKeyboardFocusSelectText(object sender, KeyboardFocusChangedEventArgs e)
            {
                TextBox textBox = e.OriginalSource as TextBox;
                if (textBox != null)
                {
                    textBox.SelectAll();
                }
            }
    
            [AttachedPropertyBrowsableForChildrenAttribute(IncludeDescendants = false)]
            [AttachedPropertyBrowsableForType(typeof(TextBox))]
            public static bool GetActive(DependencyObject @object)
            {
                return (bool) @object.GetValue(ActiveProperty);
            }
    
            public static void SetActive(DependencyObject @object, bool value)
            {
                @object.SetValue(ActiveProperty, value);
            }
        }
  • 相关阅读:
    探索Java8:(二)Function接口的使用
    Vue 动态图片加载路径问题和解决方法
    小工具:使用Python自动生成MD风格链接
    解决Navicat Premium 12 连接oracle数据库出现ORA-28547的问题
    Beetl模板引擎入门教程
    JSON.stringify()的深度使用
    nvm 查看node版本
    去掉点击a标签时产生的虚线框
    html 设置input框的记忆功能(联想内容)
    php post和get请求
  • 原文地址:https://www.cnblogs.com/akiing/p/9717270.html
Copyright © 2011-2022 走看看