• wpf 附加属性样例代码


    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using System.Text.RegularExpressions;

    namespace zl_screensaver
    {
        public static class TextBoxExtensions
        {
            public static readonly DependencyProperty IsNumericProperty =
                DependencyProperty.RegisterAttached("IsNumeric", typeof(bool), typeof(TextBoxExtensions),
                    new PropertyMetadata(false, OnIsNumericChanged));

            public static bool GetIsNumeric(DependencyObject obj)
            {
                return (bool)obj.GetValue(IsNumericProperty);
            }

            public static void SetIsNumeric(DependencyObject obj, bool value)
            {
                obj.SetValue(IsNumericProperty, value);
            }

            private static void OnIsNumericChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                TextBox textBox = d as TextBox;
                if (textBox == null) return;

                if ((bool)e.NewValue)
                {
                    textBox.PreviewTextInput += TextBox_PreviewTextInput;
                    DataObject.AddPastingHandler(textBox, OnPaste);
                    InputMethod.SetIsInputMethodEnabled(textBox, false); // 禁用输入法
                }
                else
                {
                    textBox.PreviewTextInput -= TextBox_PreviewTextInput;
                    DataObject.RemovePastingHandler(textBox, OnPaste);
                    InputMethod.SetIsInputMethodEnabled(textBox, true); // 启用输入法
                }
            }

            private static void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
            {
                if (!IsTextNumeric(e.Text))
                {
                    e.Handled = true;
                }
            }

            private static void OnPaste(object sender, DataObjectPastingEventArgs e)
            {
                if (e.DataObject.GetDataPresent(typeof(string)))
                {
                    string text = (string)e.DataObject.GetData(typeof(string));
                    if (!IsTextNumeric(text))
                    {
                        e.CancelCommand();
                    }
                }
                else
                {
                    e.CancelCommand();
                }
            }

            private static bool IsTextNumeric(string text)
            {
                Regex regex = new Regex("[^0-9]+"); // 只允许数字
                return !regex.IsMatch(text);
            }
        }
    }
     

  • 相关阅读:
    SpringBoot+Vue+Element-UI实现协同过滤算法商品推荐系统
    Vue的SetUp函数
    Looker Studio | 带来强大的探索、更新鲜的数据和更快的过滤
    Python用GAN生成对抗性神经网络判别模型拟合多维数组、分类识别手写数字图像可视化...
    SpringCloud-OAuth2(四):改造篇
    UVA524 素数环 Prime Ring Problem
    对于电商API接口详情问题-淘宝/天猫的介绍
    面试百问:项目上线后才发现bug怎么办?
    java实现 微信公众号推送消息 ,cv 就可运行!!!
    Vue Grid Layout -️ 适用Vue.js的栅格布局系统,在vue3+上使用
  • 原文地址:https://blog.csdn.net/zhang8593/article/details/139438168