zoukankan      html  css  js  c++  java
  • 让textbox获焦、失焦事件,获焦时,其text被选中

    一:让textbox获焦、失焦事件: 
    textBox1.LostFocus += new RoutedEventHandler(textBox1_LostFocus);//获焦点是GotFocus
    textBox2.GotKeyboardFocus += new KeyboardFocusChangedEventHandler(textBox2_GotKeyboardFocus);//仅键盘操作的失焦的事件,有下面的例子中有用到
    
    二:键盘操作获焦时,其中text被全选中:
    1.写个附加属性,然后就可以在Style里面用了
    View Code
    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"/>
    local是你引用的命名空间
    转载请注明出处。
  • 相关阅读:
    Flink sql 之 AsyncIO与LookupJoin的几个疑问 (源码分析)
    Flink sql 之 微批处理与MiniBatchIntervalInferRule (源码分析)
    Go学习例子(六)
    Go学习例子(五)
    Go学习例子(二)
    Go学习例子(四)
    Go学习例子(一)
    Go学习例子(三)
    cookie,session傻傻分不清楚?
    Linux服务器查看日志
  • 原文地址:https://www.cnblogs.com/Laro/p/1957036.html
Copyright © 2011-2022 走看看