SetResourceReference

Sep 4, 2008 at 8:15 PM
Hi

I am searching for a flexible localization possibility and found one from Sacha Barber  http://sachabarber.net/?p=286 (using satelite resource DLLs in WPF).

It basically binds labels to assembly items using a DynamicResource like this 

 

<Application.Resources>

 

 

<ResourceDictionary>

 

 

<ResourceDictionary.MergedDictionaries>

 

 

<ResourceDictionary Source="Dictionary_en-US.xaml" />

 

 

</ResourceDictionary.MergedDictionaries>

 

 

</ResourceDictionary>

 

 

</Application.Resources>

 

in Xaml (for a textblock text)

 

<TextBlock Text="{DynamicResource label1}"

 

 

Margin="5">

 


The resource looks like this

<

ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

 

 

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

 

 

xmlns:system="clr-namespace:System;assembly=mscorlib">

 

 

<system:String x:Key="label1">en-us label1</system:String>

 

 

<system:String x:Key="label2">en-us label2</system:String>...

 



The column headers are bound the same way 

<dg:DataGrid.Columns>

 

 

<dg:DataGridTextColumn Header="{DynamicResource label1}" DataFieldBinding="{Binding Path=ModuleName}" />

 


When starting the application all seems well, both the TextBlock and the column header get the label from the initial ressource.

But after switching the assembly the labels will only be refreshed for the TextBlock, although the resource entry is the same for both

 The switch is done using this routine:

 

Public Sub ApplyCultureDictionary(ByVal cultureDictionaryUri As Uri)

 

 

' Load the ResourceDictionary into memory.

 

 

Dim cultureDict As ResourceDictionary = TryCast(Application.LoadComponent(cultureDictionaryUri), ResourceDictionary)

 

 

Dim mergedDicts As Collection(Of ResourceDictionary) = MyBase.Resources.MergedDictionaries

 

 

' Remove the existing skin dictionary, if one exists.

 

 

' NOTE: In a real application, this logic might need

 

 

' to be more complex, because there might be dictionaries

 

 

' which should not be removed.

 

 

If mergedDicts.Count > 0 Then

 

mergedDicts.Clear()

 

End If

 

 

' Apply the selected skin so that all elements in the

 

 

' application will honor the new look and feel.

 

mergedDicts.Add(cultureDict)

 

End Sub

 


 Unfortunately, this does not work for the Header-Property 

Am I completely wrong here?

Regards
Klaus
Sep 4, 2008 at 8:34 PM
Hi

I missed one detail:

In Sachas solution, there was a problem that controls (a button) build at runtime won't get refreshed.
This may be the same here.

The following helped for the button

Button dynbtn = new Button();
dynbtn.SetResourceReference(Button.ContentProperty, “label1″);
dynPanel.Children.Add(dynbtn);

Maybe there is one such line for the gridcols, too?
Regards
Klaus