首页 > 其他分享 >WPF DataBinding: Nullable Int still gets a validation error?

WPF DataBinding: Nullable Int still gets a validation error?

时间:2022-09-19 17:25:47浏览次数:104  
标签:validation Nullable Int error WPF gets empty textbox

WPF DataBinding: Nullable Int still gets a validation error?

I have a textbox databound to a nullable int through code. If I erase the data from the textbox it gives me a validation error (red border around it).

Here is my binding code:

ZipBinding = new Binding("Zip");
ZipBinding.Source = Address;
zipTextBox.SetBinding(TextBox.TextProperty, ZipBinding);

public Int32? Zip { get { ... } set { ... } }

It's clearly marked as a Nullable so why does WPF wanna give me a validation issue when I clear the textbox?

 

回答1

Validation is failing because it can't convert the empty string to a nullable integer. Set TargetNullValue to string.empty on the Binding and it will convert the empty string to null, which will be valid. 

评论

Works great! See this answer for how to do it in XAML stackoverflow.com/a/1895482/83111 – Oskar Jan 4, 2012 at 20:58        

Set value to null in WPF binding

please take a look at the following line

<TextBox Text="{Binding Price}"/>

This Price property from above is a Decimal? (Nullable decimal).

I want that if user deletes the content of the textbox (i.e. enters empty string, it should automatcally update source with null (Nothing in VB).

Any ideas on how I can do it 'Xamly'?

 

回答1

I am using .NET 3.5 SP1 so it's very simple:

<TextBox Text="{Binding Price, TargetNullValue=''}"/>

Which stands for (thanks Gregor for your comment):

<TextBox Text="{Binding Price, TargetNullValue={x:Static sys:String.Empty}}"/>

sys is the imported xml namespace for System in mscorlib:

xmlns:sys="clr-namespace:System;assembly=mscorlib"

Hope that helped.

 

 

 

 

 

标签:validation,Nullable,Int,error,WPF,gets,empty,textbox
From: https://www.cnblogs.com/chucklu/p/16708327.html

相关文章

  • linux mint 设置 软件源
    不要直接去修改/etc/apt/或/etc/apt/source.list.d下的文件原因:mint相对ubuntdebian国内支持的源比较少手工编辑容易出错从系统管理->软件源菜单中激活软件......
  • linux mint 安装 sogo 输入法
    1在http://pinyin.sogou.com/linux/网页中下载相应版本2安装sudodpkg-isogoupinyin_4.0.1.2800_x86_64.deb正常情况下,输入法依赖包没有安装全,本次安装会提示错误......
  • mysql报错 a foreign key constraint fails(外键约束错误)
    报错信息如下:(pymysql.err.IntegrityError)(1452,u'Cannotaddorupdateachildrow:aforeignkeyconstraintfails解决办法:SETFOREIGN_KEY_CHECKS=0;#临时......
  • How To determine Linux Kernel Timer Interrupt Frequency
    HowTodetermineLinuxKernelTimerInterruptFrequencyhttps://www.advenage.com/topics/linux-timer-interrupt-frequency......
  • Code Gets New
    JDKInteger-缓存实现publicstaticIntegervalueOf(inti){if(i>=IntegerCache.low&&i<=IntegerCache.high)returnIntegerCache.......
  • 关于IntelliJ IDEA 2020.1 勾选delegate IDE build/run actions to maven后测试类方法
    今天写MAVEN项目时,在执行测试类时发现方法都执行了两次,比如我执行insertAccout的测试类,就保存了两条相同的记录,执行别的测试类的时候,都会附带执行一次插入,产生一条记录,看了......
  • Android最强布局——ConstraintLayout约束布局
    ConstraintLayout首先,现附上官方文档:ConstraintLayout官方文档约束布局ConstraintLayout是一个ViewGroup,可以在Api9以上的Android系统使用它,它的出现主要是为了解决布局......
  • IntelliJ IDEA的配置
     打开设置  文件编码  忽略代码大小写  自动导包  关闭单词拼写检查  代码截图插件   代码预览插件 ......
  • 从SAP ECC升级到SAP S4HANA, 几个Key Points
    从SAPECC升级到SAPS4HANA,几个KeyPoints  自从SAP公司的拳头产品S/4HANA横空出世以来,就引起了世界范围内的众多客户以及ERP咨询业界的强烈关注。 笔者发现很......
  • 聊聊计算机之Intel CPU的MESI协议
    1.on-chip概念on-chip:每个CPU有好几个物理核,它们分布在CPU上,称为on-chipon-chipfirstcache:每个核内的一级缓存onchipbranchtable:每个核内都有一个分支预测表2.当......