0

I have a TextBox in WPF binded to a DateTime property. Whenever a user enter a date in that TextBox, i need the binded DateTime property to read the date in dd-MM-yyyy format.

i.e when the user enter 05-04-2015, the binded DateTime Property should get DateTime value corresponding to 5th April 2015.

I did a normal binding but the property is interpreting the date as 4th May 2015 instead of 5th April 2015.

In View:

<TextBox Grid.Row="1" Grid.Column="1" Style="{StaticResource TextBoxStyle}" 
     Text="{Binding NewStaff.Insurance.ExpiryDate}"></TextBox>

In viewmodel I have a NewStaff property which refer to a Staff class instance, this staff class has a insurance class instance. Inside Insurance class I have a DateTime Property called ExpiryDate.

class Staff{

    public ICard Insurance{get;set;}

    public Staff(){

        Insurance = new Insurance();
    }

}

class Insurance:ICard
{
    /// <summary>
    /// Expiry date of insurance card.
    /// </summary>
    public DateTime ExpiryDate
    {
        get;
        set;
    }
 }

Now when I type 05-04-2015 in the textbox, the binded ExpiryDate property is interpreting the date as 4th May 2015. Whereas I need ExpiryDate to interpret it as 5th April 2015.

Adeeb Arangodan
  • 152
  • 1
  • 11
  • Duplicate of this? : http://stackoverflow.com/questions/1333052/wpf-format-datetime-in-textblock – PaulF Jun 24 '15 at 13:13
  • @LeZohan68, this is a WPF question, *not* a web related question. – Sheridan Jun 24 '15 at 13:14
  • Sorry it is not the duplicate of that question. I already referred that. Using string format will only change the date i entered into the specified format. i.e when i type 05-04-2015, and if i use stringformat then my textbox will display the date as 04-05-2015. – Adeeb Arangodan Jun 24 '15 at 13:15
  • why dont you use the DatePicker? thats what its for and eliminates confusion about formats – Theodosius Von Richthofen Jun 24 '15 at 13:29
  • I need to use this technique for converting hijri date to georgian date. will DatePicker allow dates to be read in hijri date format? – Adeeb Arangodan Jun 24 '15 at 13:35
  • Maybe [this](http://www.codeproject.com/Articles/570197/ASP-NET-DatePicker-User-Control-Hijri-Gregorian-Sh) can help you. That's a Datepicker which lets you choose the calendar type. – LzyPanda Jun 24 '15 at 13:47

1 Answers1

1

You should use the DateTime.ParseExact Method in your data bound property setter, or in an IValueConverter.Convert method. This will enable you to parse the correct DateTime value using your required format. See this example from the linked page on MSDN:

  string dateString, format;  
  DateTime result;
  CultureInfo provider = CultureInfo.InvariantCulture;

  // Parse date-only value with invariant culture.
  dateString = "06/15/2008";
  format = "d";
  try {
     result = DateTime.ParseExact(dateString, format, provider);
     Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
  }
  catch (FormatException) {
     Console.WriteLine("{0} is not in the correct format.", dateString);
  } 
Sheridan
  • 68,826
  • 24
  • 143
  • 183