在 WPF 绑定中将值设置为 null [英] Set value to null in WPF binding

查看:42
本文介绍了在 WPF 绑定中将值设置为 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请看下面一行

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

上面的这个 Price 属性是一个 Decimal?(可空的十进制).

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

我希望如果用户删除文本框的内容(即输入空字符串,它应该自动将源更新为空(VB 中没有).

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).

关于我如何Xamly"做到这一点的任何想法?

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

推荐答案

我使用的是 .NET 3.5 SP1 所以它非常简单:

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

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

代表什么(感谢 Gregor 的评论):

Which stands for (thanks Gregor for your comment):

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

sys 是为 mscorlib 中的 System 导入的 xml 命名空间:

sys is the imported xml namespace for System in mscorlib:

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

希望有所帮助.

这篇关于在 WPF 绑定中将值设置为 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆