WPF 中自定义控件及其使用

主要有4个步骤:

1. 首先创建一个自定义的控件,该控件继承 TextBox

namespace Presentation.Common
{
    /// <summary>
    /// 数字框,继承文本框,仅限数字输入,扩展 Value(decimal)
    /// </summary>
    public class ExNumericBox:TextBox
    {
        #region Dependency properties
        public int Digits
        {
            get { return (int)GetValue(DigitsProperty); }
            set { SetValue(DigitsProperty, value); }
        }
 
        public static readonly DependencyProperty DigitsProperty = DependencyProperty.Register("Digits", typeof(int), typeof(ExNumericBox), new PropertyMetadata(2));
 
        public decimal Value {
            get { return (decimal)GetValue(ValueProperty); }
            set { SetValue(ValueProperty,value); }
        }
 
        public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(decimal), typeof(ExNumericBox), new PropertyMetadata(decimal.Zero));
        
        #endregion
        public ExNumericBox()
            :base()
        {
            this.VerticalContentAlignment = VerticalAlignment.Center;
            this.TextChanged += new TextChangedEventHandler(NumericBox_TextChanged);
        }
 
        private string backupString = "";
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NumericBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox tb = (TextBox)sender;
            string temp = tb.Text.Trim();
            if (!isDecimal(temp))
            {//revert string
                tb.Text = backupString;
                tb.Select(backupString.Length, 0);
                return;
            }
            decimal tempvalue = 0;
            Decimal.TryParse(temp, out tempvalue);
 
            backupString = temp;
            Value = tempvalue;
        }
        /// <summary>
        /// 是否数字
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        bool isDecimal(string source)
        {
            foreach (char item in source)
            {
                if ((item < '0' || item > '9'))
                {
                    if (Digits == 0)
                        return false;
                    if (Digits != 0 && item != '.')
                        return false;
                }
            }
            return true;
        }
    }
}

2.引入前面自定义控件生成的dll;

3. window 或 usercontrol 类中要使用该控件时先引入命名空间,如:

xmlns:Common1="clr-namespace:Presentation.Common;assembly=Presentation.Common"

4.使用控件

 <Common1:ExNumericBox x:Name="tbFirstCost" HorizontalAlignment="Left" Height="22" Margin="38,4,0,4" TextWrapping="Wrap" VerticalAlignment="Center" Width="50" VerticalContentAlignment="Center" Grid.Column="1"/>

 

转载于:https://www.cnblogs.com/DreamRecorder/p/9582514.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值