0

Ok I would think this would be fairly simple, but it seems almost impossible to do. I have a resource dictionary in my app and I define a style like so:

<Style x:Key="appBackground" TargetType="Grid"> <Setter Property="Background" Value="#ebf2f3"/> </Style> 

My end goal would be to allow users to select a color and let them change the appearance of the app. I would like to still use my resourcedictionary to control the styles, so they can pick like background color, main text color, and accent color and remain consistent throughout the app. But I cannot find a way to edit the resourceDictionary. I have tried to use the System.Windows.Markup.XamlWriter.Save method which was talked about in this SO post (How to dynamically add key and value to the ResourceDictionary in wpf?) but when I do it adds:

<s:String x:Key="appBackground">Blue</s:String> 

Any ideas or suggestions out there??

1 Answer 1

1

You can make value of your setter a binding to static resource, that you would be dynamically changing like in the answer you referenced.

<Color x:Key="myAppBackground">#ebf2f3</Color> <Style x:Key="appBackground" TargetType="Grid"> <Setter Property="Background" Value="{StaticResource myAppBackground}"/> </Style> 

After this you would be just changing value of myAppBackground.

Another suggestion, though it might not solve your problem since it's a bit different. You still make values of setters a binding, but instead of dynamically changing the values you would have 2 ResouceDictionaries with same keys, but different values. You would merge the one you want with the rest in your application and apply that. I used this approach to load different color themes on application startup, but if you need to do this many times it might be slow and not efficient approach.

Sign up to request clarification or add additional context in comments.

1 Comment

This worked, I was able to save the changes to the XAML! Now just need to get the UI to update when the setting is changed, but that is a question for another time.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.