I try to implement DateTimeFormatCultureConverter to the DataGridTextColumn.
It works and I can debug it but it doesn’t change the DateTime format at all. So I cannot see any visible changes…
(I can always use return formated; // DateTime.Parse(formated);
But in that case to sort field by ASC/DESC doesn’t work.)
Any clue why?
Thank you!
CODE
public class DateTimeFormatCultureConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
DateTime originalValue = (DateTime)value;
CultureInfo currentUICulture = Thread.CurrentThread.CurrentUICulture;
if (currentUICulture.EnglishName.Contains("Spanish") || currentUICulture.EnglishName.Contains("Portuguese"))
{
string formated = string.Format("{0}/{1}/{2}", originalValue.Day, originalValue.Month, originalValue.Year);
return DateTime.Parse(formated);
}
else
{
string formated = string.Format("{0}/{1}/{2}", originalValue.Month, originalValue.Day, originalValue.Year);
return DateTime.Parse(formated);
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
DataGrid
<sdk:DataGridTextColumn x:Name="txcInstalled"
CanUserReorder="True"
CanUserResize="True"
CanUserSort="True"
Width="Auto"
Binding="{Binding Installed, Converter={StaticResource DateTimeFormatCultureConverter}}"
IsReadOnly="True" />
You are returning a
DateTimevariable, instead of the formatted string. Remove thereturn DateTime.Parse(formated);, and insteadreturn formated;. That should do the trick for you.Edit Without using a value converter, you can instead use the
StringFormatBinding property. See http://blogs.msdn.com/b/mikehillberg/archive/2008/05/29/trying-out-binding-stringformat.aspxThe following should give you a culture-specific string, which is what you are looking for, I think: