Click here to Skip to main content
12,827,730 members (46,262 online)
Rate this:
Please Sign up or sign in to vote.
See more: Silverlight
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.
Posted 25-Sep-12 6:12am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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";

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";

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.

krishnaprasad62 25-Sep-12 12:45pm
I am sorry Sergey, I forgot to add something in my query above. I am using MVVM pattern in my project. I am not using Event Handlers in code behind here. All my code is present in ViewModel. I am using [DefaultValue="0"] from DataAnnotations for the String property, but still it is not working. Am i missing something?
Sergey Alexandrovich Kryukov 25-Sep-12 14:18pm
No, no, I used "event" in wider sense of this word. And -- come on -- you always use the events in your application. This is not the simple console application anyway, right? You do use them. If you don't see it, this is the different story...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170326.1 | Last Updated 25 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100