主要有3个步骤:

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

namespace EzIntePark.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. window 或 usercontrol 类中要使用该控件时先引入命名空间,如:

xmlns:Common="clr-namespace:EzIntePark.Presentation.Common"

    3. 使用该控件

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

  

wpf 中自定义控件及其使用的更多相关文章

  1. 在WPF中自定义控件

    一, 不一定需要自定义控件在使用WPF以前,动辄使用自定义控件几乎成了惯性思维,比如需要一个带图片的按钮,但在WPF中此类任务却不需要如此大费周章,因为控件可以嵌套使用以及可以为控件外观打造一套新的样 ...

  2. 在WPF中自定义控件(3) CustomControl (上)

    原文:在WPF中自定义控件(3) CustomControl (上) 在WPF中自定义控件(3) CustomControl (上)                              周银辉 ...

  3. 在WPF中自定义控件(3) CustomControl (下)

    原文:在WPF中自定义控件(3) CustomControl (下)   在WPF中自定义控件(3) CustomControl (下)                                 ...

  4. 在WPF中自定义控件(1)

    原文:在WPF中自定义控件(1)    在WPF中自定义控件(1):概述                                                   周银辉一, 不一定需要自定 ...

  5. 在WPF中自定义控件(2) UserControl

    原文:在WPF中自定义控件(2) UserControl 在WPF中自定义控件(2) UserControl                                               ...

  6. [转]在WPF中自定义控件 UserControl

    在这里我们将将打造一个UserControl(用户控件)来逐步讲解如何在WPF中自定义控件,并将WPF的一些新特性引入到自定义控件中来.我们制作了一个带语音报时功能的钟表控件, 效果如下: 在VS中右 ...

  7. 在WPF中UserControl

    在这里我们将将打造一个UserControl(用户控件)来逐步讲解如何在WPF中自定义控件,并将WPF的一些新特性引入到自定义控件中来.我们制作了一个带语音报时功能的钟表控件, 效果如下: 在VS中右 ...

  8. 在WPF中减少逻辑与UI元素的耦合

    原文:在WPF中减少逻辑与UI元素的耦合             在WPF中减少逻辑与UI元素的耦合 周银辉 1,    避免在逻辑中引用界面元素,别把后台数据强加给UI  一个糟糕的案例 比如说主界 ...

  9. WPF中添加Winform用户自定义控件

    过程:创建WPF工程->创建Winform用户自定义控件工程->WPF中引用控件->添加到Xaml页面 1.首先在WPF工程的解决方案上右击选择添加新建项目: 选择Windows窗体 ...

随机推荐

  1. webclient乱码问题

    我的备注:这个方法可以得到相关页面的源代码,查看页面编码,浏览器中右键>选择编码就看到所用的编码类型了 webclient在调用DownloadData或者DownloadString的时候请求 ...

  2. 开源CMS建站项目DNN研究与心得

    DNN (Dotnetnuke) 首先是开源的,并且采用BSD开源协议,也就是说你可以任意修改源代码.传播.作为商品出售,仅有的要求就是保留源代码中的版权文字,这就解决了我多年来的心病:我知道动网新闻 ...

  3. c-参数(argument)

    In  C,  array arguments behave as though they are passed by reference, and scalar variables and cons ...

  4. MySQL 数据表修复及数据恢复

    1. MYSQL数据表在什么情况下容易损坏? 服务器突然断电导致数据文件损坏. 强制关机,没有先关闭mysql 服务等.   2. 数据表损坏后的主要现象是什么? 从表中选择数据之时,得到如下错误:I ...

  5. 微信js-sdk,选择图片,上传,下载到本地,php服务端

    //前端js代码<script> //客户端6.0.2 wx.config({ //debug:true, appId: "{pigcms:$signPackage.appId} ...

  6. MySQL如何执行关联查询

    MySQL中‘关联(join)’ 一词包含的意义比一般意义上理解的要更广泛.总的来说,MySQL认为任何一个查询都是一次‘关联’ --并不仅仅是一个查询需要到两个表的匹配才叫关联,索引在MySQL中, ...

  7. linux下定时任务

    一.linux定时任务软件种类 .at : 适合执行一次的任务.突发性的任务.需要启动 atd 服务才能执行任务. .crontab: 周期性的执行任务工作:启动crond 服务后可以执行任务.最常用 ...

  8. phpcms V9 修改生成静态文件路径/html

    在论坛看到部分用户反馈这个问题,要修改的其实是html_root的值,默认是"/html"如果要生成在网站根目录的话,这个值则要为空.论坛上现在看到的办法是打开caches\con ...

  9. FileWriter

    package file; import java.io.File; import java.io.FileWriter; import java.io.IOException; public cla ...

  10. 基于.NET MVC的高性能IOC插件化架构(一)

    最近闲下来,整理了下最近写的代码,先写写架构,后面再分享几个我自己写的插件 最近经过反复对比,IOC框架选择了Autofac,原因很简单,性能出众,这篇博文是我的各大IOC框架的性能测试:http:/ ...