运行时的Wpf本地化不会影响日期格式

时间:2012-08-23 08:30:26

标签: wpf date localization format

我在Christians Mosers Wpf tutorial中使用MarkupExtension本地化。 在运行时更改当前语言效果很好,但我发现,日期格式永远不会更新。无论选择哪种语言,它始终按en-US格式化。

我正在设置这样的当前语言

Thread.CurrentThread.CurrentUICulture = value;
Thread.CurrentThread.CurrentCulture = value;

我错过了什么吗?

3 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,但使用Advanced WPF Localization中的LocBinding和简单的DateTimeConverter解决了我的问题。因此,当我在运行时切换当前语言时,转换器 DOES 会被触发。

我的XAML看起来像:

<TextBlock Style="{StaticResource EntryBoxHeader}">
    <TextBlock.Text>
        <LocBinding StringFormat="{}{0:d}">
           <Binding Source="{x:Static System:DateTime.Now}" Path="." Converter="{StaticResource dtConvertor}"/>
        </LocBinding>
    </TextBlock.Text>
</TextBlock>

答案 1 :(得分:0)

我必须编写一个转换器,以便在XAML中正确定位我的日期(您的Thread.CurrentThread.CurrentUICultureThread.CurrentThread.CurrentCulture可能已包含正确的值!)!:

public sealed class DateTimeToStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
            return null;

        if (value is DateTime)
                // HERE YOU HAVE TO PASS YOUR CULTURE INFO:
                return ((DateTime)value).ToString("d", Thread.CurrentThread.CurrentUICulture);
        else
            throw new NotImplementedException();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
            return null;
        else
            throw new NotImplementedException();
    }
}

...然后在你的XAML中使用它(记得在资源字典中声明它),如下所示(converter:前缀与声明DateTimeToStringConverter类的命名空间相关! ):

<Window.Resources>
    <ResourceDictionary>
        <converters:DateTimeToStringConverter x:Key="DateTimeToStringConverter" />
    </ResourceDictionary>
</Window.Resources>

<TextBlock Text="{Binding Path=Date, Mode=OneWay, Converter={StaticResource DateTimeToStringConverter}}"  />

答案 2 :(得分:0)

尝试:

public static class LanguageManipulator
{
    public static void SetXmlFromCurrentCulture()
    {
        var curr = CultureInfo.CurrentCulture;
        var lang = XmlLanguage.GetLanguage(curr.Name);

        SetCulture(lang, curr);

        var meteadata = new FrameworkPropertyMetadata(lang);
        FrameworkElement.LanguageProperty.OverrideMetadata(typeof (FrameworkElement), meteadata);
    }

    private static void SetCulture(XmlLanguage lang, CultureInfo cult)
    {
        var propertynames = new[]
                                {
                                    "_equivalentCulture",
                                    "_specificCulture",
                                    "_compatibleCulture"
                                };

        const BindingFlags flags = BindingFlags.ExactBinding | BindingFlags.SetField | BindingFlags.Instance | BindingFlags.NonPublic;

        foreach (var name in propertynames)
        {
            var field = typeof (XmlLanguage).GetField(name, flags);
            if (field != null) field.SetValue(lang, cult);
        }

    }
}

设置线程文化后调用SetXmlFromCurrentCulture。