My app has support for multiple languages and those languages are stored as resource dictionaries, so changing it just a matter of loading the correct resource, no restart needed.
In one window, I need to display some text based on the ViewModel state. Rather than having multiple TextBlock
elements and switching visibility, I'm trying to return the DynamicResource
directly from my ViewModel.
I tried creating a property returning a DynamicResourceExtension
but it doesn't work, as the text returned is the name of the class, instead of the actual resource.
public DynamicResourceExtension TextResource
{
get => return new DynamicResourceExtension("String.TextResource");
}
<TextBlock Text="{Binding TextResource, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>
I also tried returning a string and setting directly to the DynamicResource
, but it also didn't work.
<TextBlock Text="{DynamicResource {Binding TextResourceKey, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}}"/>
CodePudding user response:
It's not possible to return the name of resource from the view model and have the resource resolved using pure XAML markup.
The view model should either return an already resolved string
value that you bind directly to, or you will have to write some code in the view that resolves the value of the returned resource name programmatically.
It's not possible to "bind" to the resource key of the DynamicResource
markup extension in XAML.