最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - WPF IValueConverter -- who passes the targetType and culture parameters? - Stack Overflow

programmeradmin1浏览0评论

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.

Share Improve this question asked 2 days ago dowdow 5712 gold badges6 silver badges17 bronze badges 2
  • 3 If you don't specify it in the binding by using ConverterCulture, the current value of Thread.CurrentThread.CurrentUICulture is passed in this parameter. – Paweł Łukasik Commented 2 days ago
  • @PawełŁukasik Your answer is partially incorrect. Assigning the culture of the thread does not affect the culture of the WPF converter. – EldHasp Commented 2 days ago
Add a comment  | 

1 Answer 1

Reset to default 1

Unfortunately, @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.

发布评论

评论列表(0)

  1. 暂无评论