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);
        }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值