How to use markup extensions from C # code?

Suppose I have a SomeExtensionMarkupExtension. Does anyone know how to assign this property from C # code?

That is, for example, in XAML, I have:

<TextBlock Text="{l:Translate LocalizedByMarkupExtension}" />

I want to do the same with C # code.

+3
source share
1 answer

In your example, your TranslateExtension needs to implement a constructor that takes one parameter. Therefore, you just need to pass the value to the constructor like this:

TranslateExtension ext = new TranslateExtension("LocalizedByMarkupExtension");

The parameter can be converted using the appropriate TypeConverter or a special Xaml value converter. But if you just pass strings, then the above should work.

ProvideValue, .

+1

Source: https://habr.com/ru/post/1768224/


All Articles