Spook
Spook

Reputation: 25927

Changing resource in the runtime

Let's suppose, that we've got the following dictionary:

<ResourceDictionary xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <sys:String x:Key="Test">Ala ma kota</sys:String>
</ResourceDictionary>

This dictionary is merged somewhere in custom control:

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="MyDictionary.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

How can I completely change value of the resource "Test" during application runtime from the code behind?

Upvotes: 1

Views: 1635

Answers (2)

Rohit Vats
Rohit Vats

Reputation: 81233

You can change resource from code-behind but main thing is how you are binding to that resource i.e. via StaticResource or DynamicResource.

Modify like this -

Resources["Test"] = "Ala ma kota updated";

XAML (After resource update from code behind, text value will differ for two approaches) -

<TextBlock Text="{StaticResource Test}"/>  // Will be Ala ma kota
<TextBlock Text="{DynamicResource Test}"/> // Will be Ala ma kota updated

Upvotes: 2

dev hedgehog
dev hedgehog

Reputation: 8791

If you are looking to change a resource from code behind then just access the resource like a dictionary and change desired value :)

Like this:

this.Resource["myThickness"] = new Thickness(2);

That would be it :)

Upvotes: 1

Related Questions