0

I have a WPF project, I want to set its background color dynamically, this is my XAML code

<Window.Resources>
        <SolidColorBrush x:Key="TextBoxBorderColor" Color="#FFB4A5B4"/>
        <SolidColorBrush x:Key="TextBoxForegroundColor" Color="Black"/>
        <SolidColorBrush x:Key="TextBoxBackgroundColor" Color="White"/>
</Window.Resources>
<ComboBox x:Name="cmbUserFullName" Background="{DynamicResource TextBoxBackgroundColor}" Foreground="{DynamicResource TextBoxForegroundColor}" 
                          BorderBrush="{DynamicResource TextBoxBorderColor}">
</ComboBox>

And this is my code behind


 var brush7 = FindResource("TextBoxBackgroundColor") as SolidColorBrush;
            if (!string.IsNullOrEmpty(Default.clrPckerTextBoxBackground)) brush7.Color = (Color)ColorConverter.ConvertFromString(Default.clrPckerTextBoxBackground);

            var brush8 = FindResource("TextBoxForegroundColor") as SolidColorBrush;
            if (!string.IsNullOrEmpty(Default.clrPckerTextBoxForeground)) brush8.Color = (Color)ColorConverter.ConvertFromString(Default.clrPckerTextBoxForeground);

            var brush9 = FindResource("TextBoxBorderColor") as SolidColorBrush;
            if (!string.IsNullOrEmpty(Default.clrPckerTextBoxBorder)) brush9.Color = (Color)ColorConverter.ConvertFromString(Default.clrPckerTextBoxBorder);

But it makes no change in combo box background color and border brush color. Can anybody help me know where is wrong with my codes? Thanks...

  • To add or overwrite existing resource, you need to set the value to resources itself instread of the variable obtained from the resources. In this case, this.Resources["TextBoxBackgroundColor"] = new SolidColorBrush((Color)ColorConverter.ConvertFromString(Default.clrPckerTextBoxBackground)); – emoacht Dec 20 '21 at 10:01
  • I did but; it did no effects – bername saz Dec 20 '21 at 20:01

1 Answers1

0

Try removing and adding into the current Resources like this:

this.Resources.Remove("KeyName");
this.Resources.Add("KeyName", (Color)ColorConverter.ConvertFromString("ColorValue"));

Ideally, you could group your brushes into different merged dictionaries (with repeated keys but different brush values for the SolidColorBrushes) and remove+ add them as a group to swap them at one go.

  • Can you explain more، please? – bername saz Dec 21 '21 at 07:06
  • Try something like this:- var brush7 = this.Resources["TextBoxBackgroundColor"] as SolidColorBrush; if (!string.IsNullOrEmpty(Default.clrPckerTextBoxBackground)) { this.Resources.Remove("TextBoxBackgroundColor"); this.Resources.Add("TextBoxBackgroundColor", (Color)ColorConverter.ConvertFromString(Default.clrPckerTextBoxBackground)); } – Apurv Kumar Dec 21 '21 at 12:22
  • But since you have 3 brushes only, it's fine. But if you want to do it at a larger scale, then add brush resources in different Xamls and add/remove Merged Dictionaries at application level instead of resource level, checkout this [StackOverflow Link](https://stackoverflow.com/questions/9739032/set-up-application-resources-from-code) – Apurv Kumar Dec 21 '21 at 12:32