(Disclaimer: This exception has been reported in numerous other threads, and yes, I have Googled around, trying to find a solution to my issue !!)
I have a WPF application, with a WPF Toolkit DatePicker control bound to a DateTime variable:
<UserControl x:Class="MikesApp.View.UserControls.FXRatesUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:toolkit="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit">
<toolkit:DatePicker x:Name="fxDate" VerticalAlignment="Center"
Text="{Binding Selected_FXRate_Date, Mode=TwoWay}"
Style="{DynamicResource DatePickerStyle1}" >
</toolkit:DatePicker>
Whenever I enter a date, such as 4/20/2012, the DatePicker control seems to think it's actually in UK date format (dd/MM/yyyy), and the WPFtoolkit source code throws an exception of "String was not recognised as a valid DateTime."
This abruptly stops any chance of WPF performing change notifications to other parts of the app, which need to know when this date changes.
Screenshot of VS2010 exception
I'm baffled.
My laptop is setup to display dates in the form MM/dd/yyyy, and I've specifically tried telling my app to use this US date format, first using this code:
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = "MMM-dd-yyyy";
Thread.CurrentThread.CurrentCulture.DateTimeFormat.LongDatePattern = "MMM-dd-yyyy hh:mm:ss";
Then, out of desperation, using this code:
CultureInfo ci = new CultureInfo("en-US");
ci.DateTimeFormat.SetAllDateTimePatterns(new string[] { "MM/dd/yyyy" }, 'd' );
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
But regardless of what I try, the DatePicker throws an exception whenever I choose a date which can't be parsed using dd/MMM/yyyy.
If I look at the line of code (taken from DatePicker.cs in the WPF Toolkit SDK) which throws the exception:
newSelectedDate = DateTime.Parse(text, DateTimeHelper.GetDateFormat(DateTimeHelper.GetCulture(this)));
..I've found that "DateTimeHelper.GetCulture(this)" does correctly return "en-US", but that the GetDateFormat function is getting the WRONG date format. It's somehow returning dd/MMM/yyyy, rather than MMM/dd/yyyy.
I'm really confused.
I've even tried cut'n'pasting the GetDateFormat function (taken from the following path in the WPFtoolkit source code) into my app, and tried to get it to reproduce the "wrong date format" problem... but when it's in my code, it runs fine, and parses MMM/dd/yyyy dates correctly every time.
WPFtoolkit\Calendar\Microsoft\Windows\Controls\DateTimeHelper.cs
Why would the WPFtoolkit code have a different Culture to my WPF application's code, which uses it ?
And how can I get the WPFtoolkit to use the correct Culture ?
No comments:
Post a Comment