如何将Silverlight中的字符串属性默认为零 [英] How to default a string property in silverlight to zero

查看:74
本文介绍了如何将Silverlight中的字符串属性默认为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在处理一个很小的要求,即在UI中将字符串属性绑定到Silverlight文本框.现在,无论何时用户将其保留为空白或null,我都希望在文本框中显示零作为默认值.谁能帮我一下.在此先谢谢您.

Hi everyone,

I am working on a small requirement, where I am binding a string property to Silverlight textbox in the UI. Now I want to display zero as default value in the textbox, whenever user leaves it as blank or null. Can any one help me out please. Thanks in advance.

推荐答案

什么,您需要myTextBox.Value = "0"吗?所以有什么问题?在所需的事件处理程序中执行操作.
更确切地说:
What, do you need myTextBox.Value = "0"? So, what''s the problem? Do in in the event handler you need.
More exactly:
if (string.IsNullOrEmpty(myTextBox.Value))
    myTextBox.Value = "0";


顺便说一句,无论如何,使用将无法输入null.如何?它可以是空的.我宁愿推荐


By the way, the use won''t be able to enter null anyway. How? It can be empty. I would rather recommend

if (myTextBox.Value.Trim() == string.Empty)
    myTextBox.Value = "0";



由于您永远无法使myTextBox.Value为空,因此您始终可以取消引用它并调用Trim.如果要处理的字符串可以为null,请先检查是否为null.

—SA



As you never can have myTextBox.Value to be null, you can always de-reference it and call Trim. If you are dealing with the string which can be null, check for null first.

—SA


这篇关于如何将Silverlight中的字符串属性默认为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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