WPF桌面应用ComboBox控件中英文切换

页面代码
ComboBox触发器需添加引用

        xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
<Label Content="语言" HorizontalContentAlignment="Right"></Label>
<ComboBox Name="languageComboBox" ItemsSource="{Binding languageList}"
         SelectedItem="{Binding languageListSelectedItem}">
   <i:Interaction.Triggers>
       <i:EventTrigger EventName="SelectionChanged">
           <i:InvokeCommandAction Command="此处自定" CommandParameter="{Binding ElementName=languageComboBox,Path=SelectedValue}"/>
       </i:EventTrigger>
   </i:Interaction.Triggers>
</ComboBox>

后台ComboBox绑定代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Controls.Ribbon;
using System.Runtime.CompilerServices;
using System.ComponentModel;

namespace WpfApp8
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : RibbonWindow
    {
        public event PropertyChangedEventHandler changed;
        protected void ProChanged([CallerMemberName] string proName = null)
        {
            if (this.changed != null)
            {
                this.changed?.Invoke(this, new PropertyChangedEventArgs(proName));
            }
        }
        protected virtual bool SetPro<T>(ref T str,T value,[CallerMemberName] string proName = null)
        {
            if (EqualityComparer<T>.Default.Equals(str, value))
            {
                return false;
            }
            str = value;
            ProChanged(proName);
            return true;
        }

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        private List<string> List = new List<string>();
        public List<string> languageList
        {
            get
            {
                List.Add("简体中文");
                List.Add("English");
                return List;
            }
            set
            {
                SetPro(ref List, value);
            }
        }

        private string Ss = (string)Application.Current.Resources.MergedDictionaries.Last()["语言"];//资源字典里的语言键值对
        public string languageListSelectedItem
        {
            get
            {
                return Ss;
            }
            set
            {
                SetPro(ref Ss, value);
            }
        }
    }
}

语言切换资源字典,其实是合并资源字典,写在App.xaml里,默认是最后一个,此处为中文

<Application x:Class="WpfApp8.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp8"
             xmlns:controls="using:Microsoft.UI.Xaml.Controls"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Dictionary1.xaml"/>
                <ResourceDictionary Source="English.xaml"/>
                <ResourceDictionary Source="Chinese.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

切换语言方法:实则为将合并的资源字典进行重新排序,因为默认是取到最后一个资源字典里的key值

private void CommandChangedLanguage(object sender,ExecutedRoutedEventArgs e)
        {
            List<ResourceDictionary> resourceDictionariesList = new List<ResourceDictionary>();
            foreach (ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries)
            {
                resourceDictionariesList.Add(dictionary);
            }
            string req = @"Chinese.xaml";
            switch (e.Parameter)
            {
                case "简体中文":
                    req = @"Chinese.xaml";
                    break;
                case "English":
                    req = @"English.xaml";
                    break;
                default:
                    break;
            }
            ResourceDictionary resourceDictionary = resourceDictionariesList.FirstOrDefault(d => d.Source.OriginalString.Equals(req));
            Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary);
            Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
        }
好的,以下是WPF动态切换中英文界面的代码: 首先,在App.xaml.cs文件中添加一个静态属性,用于存储当前的语言选项。 ```csharp public static string Language { get; set; } = "en-US"; ``` 接着,在App.xaml文件中添加资源字典,并设置其Source属性为当前语言选项对应的资源文件。 ```xaml <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Resources\en-US.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> ``` 然后,在主窗口中添加一个语言切换ComboBox控件,并为其SelectedValue属性绑定当前语言选项。 ```xaml <ComboBox x:Name="LanguageComboBox" SelectedValue="{Binding Source={x:Static Application.Current}, Path=Language}"> <ComboBoxItem Content="English" Tag="en-US" /> <ComboBoxItem Content="中文" Tag="zh-CN" /> </ComboBox> ``` 接下来,在主窗口的代码中,为语言切换ComboBox控件的SelectionChanged事件添加处理方法,用于动态切换界面语言。 ```csharp private void LanguageComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { var selectedItem = (ComboBoxItem)LanguageComboBox.SelectedItem; var language = (string)selectedItem.Tag; App.Language = language; // 切换资源文件 var dictionary = new ResourceDictionary(); dictionary.Source = new Uri($@"Resources\{language}.xaml", UriKind.RelativeOrAbsolute); Application.Current.Resources.MergedDictionaries.Clear(); Application.Current.Resources.MergedDictionaries.Add(dictionary); } ``` 最后,在资源文件夹中添加两个资源文件,分别对应英文和中文界面。例如,en-US.xaml文件的内容如下: ```xaml <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <SolidColorBrush x:Key="BackgroundColor" Color="#FFFFFF" /> <SolidColorBrush x:Key="ForegroundColor" Color="#000000" /> </ResourceDictionary> ``` zh-CN.xaml文件的内容如下: ```xaml <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <SolidColorBrush x:Key="BackgroundColor" Color="#000000" /> <SolidColorBrush x:Key="ForegroundColor" Color="#FFFFFF" /> </ResourceDictionary> ``` 这样就实现了WPF动态切换中英文界面的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值