Click here to Skip to main content
15,792,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

In my WPF test project I tried to make the style dynamically changeable through a combobox. Everything works except the background color of the windows.

Can you help me?

The source code is here.
Thank you!

What I have tried:

I have tried with StaticResource, with SolidColorBrush, with the same name, with different name of static resource but it does not work!
Updated 1-Jul-21 4:39am
CHill60 1-Jul-21 8:29am    
I am unable to follow that link. Post the relevant code in your question - you can use the "Improve Question" link to add the information
Richard Deeming 1-Jul-21 8:52am    
Try using DynamicResource instead:
Style="{DynamicResource BaseStyle}"

1 solution

Richard Deeming is correct, you've used the DynamicResource to allocate the styles to the elements, but the styles themselves are using StaticResource to allocate the colours. You need to amend the styles themselves to use dynamic resources:

<Style x:Key="BaseStyle" TargetType="{x:Type Control}" >
    <Setter Property="Background" Value="{DynamicResource FormBackground}" />

A StaticResource is resolved and assigned when the style is loaded, and any changes to that resource won't be reflected. The DynamicResource uses an expression to resolve the value, so changes can be reflected.
Share this answer
Red.Devlab 2-Jul-21 4:58am    
Yesss!!! I have changed from Static to Dynamic and it's works! Thank you for your help!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900