Click here to Skip to main content
Rate this: bad
good
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
good
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.
 
—SA
  Permalink  
v2
Comments
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...
--SA

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

  Print Answers RSS
0 OriginalGriff 239
1 Sarvesh Kumar Gupta 185
2 George Jonsson 154
3 ArunRajendra 144
4 Richard MacCutchan 140


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