Answered by:
How to set null value to a Property when a TextBox value has been cleared up

Question
-
Hi,I have Property of Data Type DateTime, which I am binding to my Textbox . when I first set it's value and leave the focus from the textbox, in the property i could see the exact value of the textbox in the property.But when I remove the entered DateTime value from the textbox, the value of the property I assume should be set to NULL but the property still persists with old datetime value of the textbox.
Is there any way to set NULL value when I remove the datetime from the textbox?
I don't want to use if condition in XAML.cs file which will check if textbox is blank and then set property to NULL.Does anybody have idea on how to achieve this?
Does TextBox control in WPF natively support to set null value instead of ""?
Thanks & Regards,
Deepak.Friday, September 26, 2008 3:48 PM
Answers
-
DateTime is a Value type. When you set it to Null it's actual value will be set to it's MinValue property.
An alternative would be to change the DataType of your DateTime property to Nullable<DateTime>.
That should allow you to set it's value to null.
Hope this helps,
Keith- Proposed as answer by Rahul P Nath Tuesday, September 30, 2008 6:22 AM
- Marked as answer by Jim Zhou - MSFT Tuesday, September 30, 2008 9:35 AM
Tuesday, September 30, 2008 6:18 AM -
Hi,
DateTime is a non-nullable type.So i think it cannot be set with a null value.I think u can set it to some default value of your choice whenever textbox is left blank
Rahul
FEAR NOT TO BE JUST- Proposed as answer by Rahul P Nath Tuesday, September 30, 2008 6:16 AM
- Marked as answer by Jim Zhou - MSFT Tuesday, September 30, 2008 9:35 AM
Tuesday, September 30, 2008 6:14 AM
All replies
-
hi,
Could you please provide a small, complete and ready-to-run example to demonstrate the issue you are encountering? so we can understand your meaning to provide some appropriate solutions.
Thanks
Jim Zhou -MSFTTuesday, September 30, 2008 6:05 AM -
Hi,
DateTime is a non-nullable type.So i think it cannot be set with a null value.I think u can set it to some default value of your choice whenever textbox is left blank
Rahul
FEAR NOT TO BE JUST- Proposed as answer by Rahul P Nath Tuesday, September 30, 2008 6:16 AM
- Marked as answer by Jim Zhou - MSFT Tuesday, September 30, 2008 9:35 AM
Tuesday, September 30, 2008 6:14 AM -
DateTime is a Value type. When you set it to Null it's actual value will be set to it's MinValue property.
An alternative would be to change the DataType of your DateTime property to Nullable<DateTime>.
That should allow you to set it's value to null.
Hope this helps,
Keith- Proposed as answer by Rahul P Nath Tuesday, September 30, 2008 6:22 AM
- Marked as answer by Jim Zhou - MSFT Tuesday, September 30, 2008 9:35 AM
Tuesday, September 30, 2008 6:18 AM