我目前正在开发一个多语言应用,用户可以根据所选语言在运行时交换界面文本。我使用在ResourceDictionary中定义的DynamicResources,并在改变语言时交换字典文件。这对于除了DataGrid的Column Header属性之外的所有内容都非常有效。我知道DataGrid列不是Visual Tree的一部分,并且过去曾经用代理绑定到VM中的属性,但在这种情况下没有绑定到VM。在交换ResourceDictionary时,如何更新列标题?
下面是我交换字典的方法。它位于Application.xaml.vb中,在应用程序启动时调用,传递保存在MySettings.Default中的字符串。还可以使用来自绑定到ComboBoxSelectedIndex的VM属性的messenger调用它。
下面是我交换字典的方法。它位于Application.xaml.vb中,在应用程序启动时调用,传递保存在MySettings.Default中的字符串。还可以使用来自绑定到ComboBoxSelectedIndex的VM属性的messenger调用它。
Private Sub SetLanguage(ByVal language As String)
Dim dic As ResourceDictionary = Nothing
Dim langFile As String = Environment.CurrentDirectory & "\Languages\" & language & ".xaml"
If File.Exists(langFile) Then
Using fs As FileStream = New FileStream(langFile, FileMode.Open)
dic = CType(XamlReader.Load(fs), ResourceDictionary)
If LanguageCount > 0 Then
Resources.MergedDictionaries.RemoveAt(Resources.MergedDictionaries.Count - 1)
End If
Resources.MergedDictionaries.Add(dic)
End Using
End If
LanguageCount += 1
End Sub
相关的 DataGrid xaml
<DataGridTextColumn Header="{DynamicResource G_Spec}" ... />
ResourceDictionary条目
<system:String x:Key="G_Spec">Spec:</system:String>