小编use*_*477的帖子

DataGrid - 在运行时更改列标题

我目前正在开发一个多语言应用程序,其中界面文本可以在运行时根据用户选择的语言进行交换.我正在使用ResourceDictionary中定义的DynamicResources并在语言更改时交换字典文件.这适用于除DataGrid的Column Header属性之外的所有内容.我知道DataGrid列不是Visual Tree的一部分,并且过去曾使用代理绑定到我的VM中的属性,但在这种情况下,没有绑定到VM.在交换ResourceDictionary时,如何更新列标题?

我的交换字典的方法如下.它驻留在Application.xaml.vb中,并在应用程序启动时调用,并传递MySettings.Default中保存的字符串.这也是使用来自VM中属性的信使调用,该信息绑定到ComboBoxSelectedIndex.

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
Run Code Online (Sandbox Code Playgroud)

相关的DataGrid xaml

<DataGridTextColumn Header="{DynamicResource G_Spec}" ... />
Run Code Online (Sandbox Code Playgroud)

ResourceDictionary条目

<system:String x:Key="G_Spec">Spec:</system:String>
Run Code Online (Sandbox Code Playgroud)

wpf xaml datagrid

4
推荐指数
1
解决办法
1402
查看次数

标签 统计

datagrid ×1

wpf ×1

xaml ×1