【发布时间】:2014-06-11 15:10:08
【问题描述】:
我目前正在开发一个多语言应用程序,其中界面文本可以在运行时根据用户选择的语言进行交换。我正在使用 ResourceDictionary 中定义的 DynamicResources 并在更改语言时交换字典文件。这适用于除 DataGrid 的 Column Header 属性之外的所有内容。我知道 DataGrid 列不是可视树的一部分,并且过去曾使用代理绑定到我的 VM 中的属性,但是,在这种情况下,没有绑定到 VM。交换 ResourceDictionary 时如何更新列标题?
我的字典交换方法如下。这驻留在 Application.xaml.vb 中,并在应用启动时调用,传递保存在 MySettings.Default 中的字符串。这也称为使用绑定到 ComboBoxSelectedIndex 的 VM 中的属性的信使。
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>
【问题讨论】:
-
向我们展示您如何“交换”该资源字典的代码。