Click here to Skip to main content
11,640,626 members (58,690 online)
Rate this: bad
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 5: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 at 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 at 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
0 Mika Wendelius 370
1 OriginalGriff 205
2 Afzaal Ahmad Zeeshan 130
3 CPallini 125
4 DamithSL 105
0 Mika Wendelius 370
1 OriginalGriff 205
2 CPallini 125
3 DamithSL 105
4 Afzaal Ahmad Zeeshan 70

Advertise | Privacy | Mobile
Web03 | 2.8.150731.1 | Last Updated 25 Sep 2012
Copyright © CodeProject, 1999-2015
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