You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF .NET Core 5运行时更改应用区域文化后如何更新属性绑定

问题根因

  1. 你当前实现的CultureAwareBinding仅在实例化阶段读取一次CultureInfo.CurrentCulture赋值给ConverterCulture,后续运行时切换文化后该属性不会自动更新,已创建的绑定实例也不会主动重新格式化DateTime值。
  2. WPF绑定默认不会监听线程文化变更事件,因此你修改当前线程文化后,已生效的绑定不会触发刷新逻辑。

解决步骤

第一步:新增全局文化变更通知

在App.xaml.cs中添加静态文化变更事件,切换文化完成后触发该事件通知所有绑定更新:

public partial class App : Application
{
    // 新增全局文化变更事件
    public static event EventHandler CultureChanged;

    public App()
    {
        CultureInfo CultureInformation = new CultureInfo("en-UK");
        CultureInformation.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
        CultureInformation.DateTimeFormat.LongDatePattern = "ddd, dd/MM/yyyy";
        CultureInfo.DefaultThreadCurrentCulture = CultureInformation;
        CultureInfo.DefaultThreadCurrentUICulture = CultureInformation;
    }
}

修改MainWindow.xaml.cs中的UpdateLanguage方法,切换文化完成后触发事件,同时同步更新默认线程文化配置:

private void UpdateLanguage(string Language)
{
    LanguageComboBox.SelectedValue = Properties.Settings.Default.Language = Language;
    Properties.Settings.Default.Save();
    ResourceDictionary Dictionary = new();
    Dictionary.Source = new Uri(@$"..\Languages\{Language}.xaml", UriKind.Relative);
    Resources.MergedDictionaries.Clear();
    Resources.MergedDictionaries.Add(Dictionary);
    
    CultureInfo CultureInformation;
    if (Language == "العربية")
    {
        CultureInformation = CultureInfo.CreateSpecificCulture("ar-EG");
        CultureInformation.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
        CultureInformation.DateTimeFormat.LongDatePattern = "ddd, dd/MM/yyyy";
    }
    else
    {
        CultureInformation = CultureInfo.CreateSpecificCulture("en-UK");
        CultureInformation.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
        CultureInformation.DateTimeFormat.LongDatePattern = "ddd, dd/MM/yyyy";
    }

    // 同步更新当前线程和默认线程的文化配置
    Thread.CurrentThread.CurrentCulture = CultureInformation;
    Thread.CurrentThread.CurrentUICulture = CultureInformation;
    CultureInfo.DefaultThreadCurrentCulture = CultureInformation;
    CultureInfo.DefaultThreadCurrentUICulture = CultureInformation;

    // 触发文化变更事件,通知所有绑定更新
    App.CultureChanged?.Invoke(null, EventArgs.Empty);
}

第二步:修改CultureAwareBinding实现

让自定义绑定监听全局文化变更事件,收到通知后自动更新转换文化并刷新绑定目标:

public class CultureAwareBinding : Binding
{
    public CultureAwareBinding()
    {
        ConverterCulture = CultureInfo.CurrentCulture;
        // 监听文化变更事件
        App.CultureChanged += (sender, e) =>
        {
            ConverterCulture = CultureInfo.CurrentCulture;
            // 强制刷新绑定目标值
            Expression?.UpdateTarget();
        };
    }
}

补充说明

如果你的DateTime格式不需要固定为hh:mm tt,可以直接使用系统提供的标准时间格式串(比如t代表短时间格式),会自动适配不同文化的显示规则,修改XAML绑定即可:

<TextBlock Text="{local:CultureAwareBinding Path=StartTime, StringFormat=t}"/>

内容的提问来源于stack exchange,提问作者Ahmed Alayat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 03:06:02