WPF: Dynamic MarkupExtension- ReRendering/Updating of value
Asked Answered
D

2

8

I´m using a markupextension for loading internationalized strings in a WPF application like this:

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

My markupextension is named "TranslateExtension" and it searches a value for the key "MyText" from a database. It does this in the

ProvideValue(IServiceProvider serviceProvider)

method which returns the right string. Everything is working fine.

My problem is, that the ProvideValue-Method isn´t ever called again and there´s no way to pull a new string from the database when the language is changed. I now need a way to make the returned value "dynamic", to make the Button to reload it´s xaml and re-use the markupextension whether it´ll be through an event thrown when changing languages or whatever. How do i make the system call the ProvideValue-Method again? I tried the likes of InvalidateVisual() InvalidateArrange() InvalidateMeasure() UpdateLayout()...

I hope I made myself clear. Please feel free to ask for more information of you think you´re able to provide ideas or solutions. Thank you

Dipeptide answered 14/6, 2009 at 14:21 Comment(0)
S
6

Well, solution is quite simple idea.

ProvideValue is called only once, but serviceProvider parameter provides you also object instance and property (PropertyInfo). This is all you need to update the propety later.

I would make an static event in App.cs, let's say CultureChanged. In ProvideValue, get IProvideValueTarget service from ServiceProvider, from which you can get the object instance and property. Attach to CultureChanged event and in its EventHandler set the property value using reflection. Do be mindful of memory leaks with.

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

Stevana answered 3/8, 2011 at 11:57 Comment(0)
M
0

I don't have an answer for you, but rather a suggestion...

Perhaps you could consider using a Binding combined with an IValueConverter to do the translation. Then, if the language of the user interface changes, you can simply recurse all bindings and refresh them.

Like I said, just an idea.

Musclebound answered 14/6, 2009 at 18:11 Comment(2)
Hello Mark, thank you for your suggestion. I´ll have a look into using bindings but they might not work in my case. the whole thing is a bit more difficult and i simplified what i needed. thing is, i´m making the languages chosable for every form separately and if the form doesn´t have a chosen language it´s going to rely on the standard language (which also can be changed). I found out, that the markup extensions are exposed after the xaml gets rendered (except when there´s still a reference somewhere). I´m currently trying bubble and tunnel events and registering a handler on the form.Dipeptide
but i´ll have to evaluate performance and memory issues. anyway, it seems like there isn´t an "really reload the form"-method anywhere near.Dipeptide

© 2022 - 2024 — McMap. All rights reserved.