In my case:
I have a TextBlock Binding to a property of type DateTime. I want it to be displayed as the Regional settings of the User says.
You can create a subclass of binding (e.g. CultureAwareBinding) which sets the ConverterCulture automatically to the current culture when created.
It's not a perfect solution, but it's probably the only one, since retroactively forcing Binding to respect the culture could break other code in WPF which depends on this behavior.
Let me know if you need more help!
Your second attempt was close, and led me to a solution that does work for me.
The problem with setting the ConverterCulture is that it is only used when you have a Converter. So simply create a simple StringFormatConverter that takes the format as its parameter:
public sealed class StringFormatConverter : IValueConverter
{
private static readonly StringFormatConverter instance = new StringFormatConverter();
public static StringFormatConverter Instance
{
get
{
return instance;
}
}
private StringFormatConverter()
{
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return string.Format(culture, (string)parameter, value);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
Then you can adjust your binding (assuming you've imported the converter's namespace as "my")
<TextBlock Text="{Binding Date, Converter={x:Static my:StringFormatConverter.Instance}, ConverterCulture={x:Static glob:CultureInfo.CurrentCulture}, ConverterParameter={}{0:d}}" />
The problem that is avoiding using "this.Language = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentCulture.Name);" is not really a common one. I don't know any user here in france that will change the date format to US or Japan one, just because at least no user is knowing that such a change is possible (and dont know how to do it)... So of course the "language=" is not perfect, but in many many years of WPF and Silverlight practice I never see a problem of this kind with any user... So I still use the "Langage=" trick, it is simple and cover 100% of real needs. Of course others solutions seem to be better, but there is no need for (and I saw a few implementations that are far from perfect compare to "language=" solution).
We can create a DateTime Converter using the IValueConverter
[ValueConversion(typeof(DateTime), typeof(String))]
class DateTimeToLocalConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!(value is DateTime)) return "Invalid DateTime";
DateTime DateTime = (DateTime)value;
return DateTime.ToLocalTime().ToShortDateString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Apply this in the XAML as shown below
Binding="{Binding Path=createdDateTime,Converter={StaticResource DateTimeConverter}}"
Also change the current culture to get the desired format and the same needs to be applied on the application startup
/// <summary>
/// Set Culture
/// </summary>
private void SetCulture() {
var newCulture = new CultureInfo("en-IN");
newCulture.DateTimeFormat.ShortDatePattern = "dd-MMM-yyyy";
newCulture.DateTimeFormat.LongDatePattern = "dd-MMM-yyyy";
newCulture.DateTimeFormat.FullDateTimePattern = "dd-MMM-yyyy";
CultureInfo.DefaultThreadCurrentCulture = newCulture;
CultureInfo.DefaultThreadCurrentUICulture = newCulture;
Thread.CurrentThread.CurrentCulture = newCulture;
Thread.CurrentThread.CurrentUICulture = newCulture;
FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(
System.Windows.Markup.XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
}
Put the following line of code, before any UI is initialized. This worked for me.
FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement),
new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
(And remove all explicit culture parameters)
How about changing the lanaguge in the code behind?
this.Language = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentCulture.Name);