WPF 如何实现动态创建内容的多语言功能

1.基本情况如题。
2.例如,树控件绑定的内容,datagridview绑定的内容,都是软件后期编辑生成的,这样如何实现这些内容的多语言模式。

  1. 在WPF中国际化使用的是 .xaml文件的格式

img

<Window x:Class="LanTest.MainWindow"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         Title="MainWindow" Height="350" Width="525">
     <Grid>
         <!--这里的{DynamicResource OK}就是动态调用 资源中的key为OK的内容-->
         <Button Content="{DynamicResource OK}" HorizontalAlignment="Left" Margin="134,161,0,0" VerticalAlignment="Top" Width="104" Height="38"/>
         <Button Content="{DynamicResource Cancel}" HorizontalAlignment="Left" Margin="278,161,0,0" VerticalAlignment="Top" Width="100" Height="38"/>
         <Button Content="Button" HorizontalAlignment="Left" Margin="287,59,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click" Loaded="Button_Loaded"/>
         <ComboBox Name="cbLang" HorizontalAlignment="Left" Margin="118,59,0,0" VerticalAlignment="Top" Width="120">
         </ComboBox>
 
     </Grid>
 </Window>


using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LanTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        //定义ComboBox选项的类,存放Name和Value
        public class CategoryInfo
        {
            public string Name
            {
                get;
                set;
            }
            public string Value
            {
                get;
                set;
            }

        }

        //切换语言
        private void btnChangeLang_Click(object sender, RoutedEventArgs e)
        {
            object selectedName = cbLang.SelectedValue;
            if (selectedName != null)
            {
                string langName = selectedName.ToString();
                //英语的语言文件名为:DefaultLanguage,所有这里要转换一下
                if (langName == "en_US")
                    langName = "DefaultLanguage";
                //根据本地语言来进行本地化,不过这里上不到
                //CultureInfo currentCultureInfo = CultureInfo.CurrentCulture;

                ResourceDictionary langRd = null;
                try
                {
                    //根据名字载入语言文件
                    langRd = Application.LoadComponent(new Uri(@"lang" + langName + ".xaml", UriKind.Relative)) as ResourceDictionary;
                }
                catch(Exception e2)
                {
                    MessageBox.Show(e2.Message);
                }

                if (langRd != null)
                {
                    //如果已使用其他语言,先清空
                    if (this.Resources.MergedDictionaries.Count > 0)
                    {
                        this.Resources.MergedDictionaries.Clear();
                    }
                    this.Resources.MergedDictionaries.Add(langRd);
                }
            }
            else
                MessageBox.Show("Please selected one Language first.");
        }

        //控件载入时,为ComboBox赋值
        private void cbLang_Loaded(object sender, RoutedEventArgs e)
        {
            List<CategoryInfo> categoryList = new List<CategoryInfo>();
            categoryList.Add(new CategoryInfo() { Name = "English", Value = "en_US" });
            categoryList.Add(new CategoryInfo() { Name = "中文", Value = "zh_CN" });

            cbLang.ItemsSource = categoryList;//绑定数据,真正的赋值
            cbLang.DisplayMemberPath = "Name";//指定显示的内容
            cbLang.SelectedValuePath = "Value";//指定选中后的能够获取到的内容
        }
    }
}

WPF实现无刷新动态切换多语言(国际化)
http://t.zoukankan.com/tommy-huang-p-4613916.html