Let's say I have a very simple converter for DateTime
that I want to display according to some CultureInfo
.
[ValueConversion(typeof(DateTime), typeof(string))]
public class DateTimeToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
// Implementation
}
// ConvertBack...
}
My question is who passes, e.g., the CultureInfo
parameter? How can I control the value being passed? Is it some configuration happening on the App.xaml.cs
level?
Right now I simply ignore that argument and use my own CultureInfo
object, but I'd like to be able to control it on a higher level, say as a global setting/variable/configuration.
Let's say I have a very simple converter for DateTime
that I want to display according to some CultureInfo
.
[ValueConversion(typeof(DateTime), typeof(string))]
public class DateTimeToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
// Implementation
}
// ConvertBack...
}
My question is who passes, e.g., the CultureInfo
parameter? How can I control the value being passed? Is it some configuration happening on the App.xaml.cs
level?
Right now I simply ignore that argument and use my own CultureInfo
object, but I'd like to be able to control it on a higher level, say as a global setting/variable/configuration.
1 Answer
Reset to default 1Unfortunately, @PawełŁukasik's answer is not entirely correct. Changing the thread culture, including for UI, does not affect the culture passed to the converter (at least for Wind 10).
The culture can be passed to the binding via the ConverterCulture parameter, or the language can be set for FrameworkElement via the Language property. Setting the language affects both the element itself and its child elements.
Test example:
public class GetCulture : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return culture.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
App.xaml.cs
public partial class App : Application
{
public App()
{
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("ru-ru");
}
App.xaml
<Application x:Class="CFTopics2025.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CFTopics2025"
StartupUri="/MainWindow.xaml">
<Application.Resources>
<local:GetCulture x:Key="getCulture"/>
</Application.Resources>
</Application>
MainWindow.xaml
<StackPanel>
<TextBlock Text="{Binding Converter={StaticResource getCulture}}"/>
<TextBlock Text="{Binding Converter={StaticResource getCulture}, ConverterCulture=ru-ru}"/>
<TextBlock Text="{Binding Converter={StaticResource getCulture}}" Language="ru-Ru"/>
<Grid Language="ru-Ru">
<TextBlock Text="{Binding Converter={StaticResource getCulture}}"/>
</Grid>
</StackPanel>
Output:
You can also use the xml:lang
attribute in XAML.
ConverterCulture
, the current value ofThread.CurrentThread.CurrentUICulture
is passed in this parameter. – Paweł Łukasik Commented 2 days ago