WPF: dynamic markup - retransmit / update value

I use markupextension to load internationalized strings in a WPF application as follows:

<Button Content="{Translate MyText}"/> 

My markupextension is called "TranslateExtension", and it searches for the value for the "MyText" key from the database. He does it in

 ProvideValue(IServiceProvider serviceProvider) 

which returns the correct string. Everything is working fine.

My problem is that the ProvideValue method no longer calls, and there is no way to fetch a new row from the database when the language changes. Now I need a way to make the return value β€œdynamic” in order to get Button to reload its xaml and reuse the markup extension, whether it will be through an event created when a language change or something else. How to call the ProvideValue method system call again? I tried similar InvalidateVisual () InvalidateArrange () InvalidateMeasure () UpdateLayout () ...

I hope I get it. Please feel free to ask more about what you think you can provide ideas or solutions. Thanks you

+6
markup wpf xaml dynamicresource
source share
2 answers

Well, the solution is a pretty simple idea.

ProvideValue is called only once, but the serviceProvider parameter also provides you with an instance of the object and a property (PropertyInfo). That is all you need to update later.

I would make a static event in App.cs, say CultureChanged; In the ProvideValue method, get an instance of the object and property and save them. attaches to the CultureChanged event and in its EventHandler sets the value of the property using reflection. that is all, be careful with memory.

There is a MarkupExtension that takes this approach and allows you to cache the culture on the fly: http://www.codeproject.com/KB/WPF/WpfMultiLanguage.aspx

+4
source share

I have no answer for you, but rather a suggestion ...

Perhaps you could consider using bindings in conjunction with IValueConverter for translation. Then, if the user interface language changes, you can simply overwrite all the bindings and update them.

As I said, just an idea.

-one
source share

All Articles