zoukankan      html  css  js  c++  java
  • 一个WPF只能输入数字的行为。

    没啥好说的,直接上代码:

     public class NumberInputBehaviour : Behavior<TextBox>
        {
            protected override void OnAttached()
            {
                base.OnAttached();
                InputMethod.SetIsInputMethodEnabled(AssociatedObject, false);
                AssociatedObject.PreviewTextInput += AssociatedObject_PreviewTextInput;
                AssociatedObject.TextChanged += AssociatedObject_TextChanged;
                AssociatedObject.LostFocus += AssociatedObject_LostFocus;
            }
    
            protected override void OnDetaching()
            {
                base.OnDetaching();
                AssociatedObject.PreviewTextInput -= AssociatedObject_PreviewTextInput;
                AssociatedObject.TextChanged -= AssociatedObject_TextChanged;
                AssociatedObject.LostFocus -= AssociatedObject_LostFocus;
            }
    
            private void AssociatedObject_LostFocus(object sender, RoutedEventArgs e)
            {
                if (string.IsNullOrEmpty(AssociatedObject.Text))
                {
                    if (!string.IsNullOrEmpty(NullText))
                    {
                        AssociatedObject.Text = NullText;
                    }
                }
            }
    
            private void AssociatedObject_TextChanged(object sender, TextChangedEventArgs e)
            {
                if (Convert.ToInt32(AssociatedObject.Text) == 0)
                {
                    AssociatedObject.Text = "500";
                    AssociatedObject.SelectionStart = AssociatedObject.Text.Length;
                }
            }
    
            private void AssociatedObject_PreviewTextInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
            {
                Regex re = new Regex("[^0-9]+");
                e.Handled = re.IsMatch(e.Text);
    
             
            }
    
    
    
            public String NullText
            {
                get { return (String)GetValue(NullTextProperty); }
                set { SetValue(NullTextProperty, value); }
            }
    
            // Using a DependencyProperty as the backing store for NullText.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty NullTextProperty =
                DependencyProperty.Register("NullText", typeof(String), typeof(NumberInputBehaviour), new PropertyMetadata(""));
    
    
        }

    xaml 使用方法 :

     <TextBox>
        <i:Interaction.Behaviors>
         <local:NumberInputBehaviour NullText="请输入"></local:NumberInputBehaviour>
        </i:Interaction.Behaviors>
     </TextBox>

    OK....

  • 相关阅读:
    eclipse导入github项目
    深入理解BFC和Margin Collapse
    前端开发必备!Emmet使用手册
    Backbone.js的技巧和模式
    智能选择器和语义化的CSS
    IE常见BUG总结(持续更新)
    表格元素的完全指南(译)
    display:inline-block;在各浏览器下的问题和终极兼容办法
    float的深入剖析
    javascript正则表达式小技巧
  • 原文地址:https://www.cnblogs.com/yk250/p/10044324.html
Copyright © 2011-2022 走看看