Question

I have a control that we could identify as similar to ListBox control. Each item is represented with one element (example TextBlock). What i would like is to change the layout of this item, so that it contains two TextBlocks. So I create a ControlTemplate, put a Border Grid, TwoTextBlocks, and all is well. Now the problem:

I need to be able to localize the text in the item, and I did this normally like this:

<... Text="{Binding Strings.SomeString, Source={StaticResource ApplicationResources}}" />

Now I need to be able to do the same with both TextBlocks. So i thought I need to create a custom type that this item will bind to, and expose two propertiws: Title and Description. If I expose this properties as string type, everything works ok, but I am loosing markup binding that I used previously. How to achieve the same with two properties? The result should be like:

<... Title="{Binding Strings.SomeString, Source={StaticResource ApplicationResources}}", Description="{Binding Strings.AnotherString, Source={StaticResource ApplicationResources}}" />

I was able to make Localization work with ResourcemManager class, but it gets even complicated in order to provide localization to be applied dynamically at runtime.

So, what do I need to do to be able to use above code? Then I just need to implement INotifyPropertyChanged on ApplicationResource and all is set.

Was it helpful?

Solution

Great!

I'm going to do the same thing you did here. Yeah, I have a solution but I'm not sure if it works till now.

First, we need a LocalizationManager which holds a dictionary.

For example, if you need to localize a user account window, just do this

<TextBlock Text="something, UsernameKey">

And the localizationManager will map UsernameKey to "Username" or other language

Second, a xaml extension which find the value of the key from LocalizationManager.

I wonder if this custom extension could derived from Binding extension, if so, this'll be very easy, just create a Binding Object to the target. If not, I think holding a WEAK reference to the UIElement by xaml extension to dynamic update the text is proper.

This solution is simple but not generic. There're some language read from right to left. It asks the application to show content from right to left.

So, I have another generic solution but more complex.

Instead of xaml extension, we use an attach dependency property.

Do it like this:

<TextBlock LocalizationManager.LocalizationKey="UsernameKey" />

So, the problem now is how to set "Text" property by LocalizationManager?

We use adapters, LocalizationManager will search proper adapter for type "TextBlock"

So, when the application is booting, we register some adapters to LocalizationManager:

LocalizationManager.Current.RegisterAdapter<TextBlock>(new TextBlockAdapter())

This solution is more generic, it supports any kind of control if you provide adapter, but as you see, this solution needs more work and much more complex than the former one.

I hope these design solutions could help you~

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top