页面代码
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);
}