1

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?

2
  • I forgot to mention, that I want to do it from the codebehind. I know, that I can manually create binding, but I suspect, there's another, simpler way :) Commented Dec 3, 2013 at 18:43
  • If you asking to change that string from code behind then take a look at my answer :) Commented Dec 3, 2013 at 18:46

2 Answers 2

2

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 
Sign up to request clarification or add additional context in comments.

Comments

1

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 :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.