Reputation: 593
i have a problem that was asked here, but given solutions didn't help.
I want to change background color of MainWindow's Grid
from another window throught App global resources. The problem is, than i change backBrush
dynamically, nothing happens. Moreover color initially is transparent. I tried different approaches of setting value in resources, like Application.Current.Resources["key"] = new_value
and
MainGrid.SetResourceReference(Grid.BackgroundProperty, "key")
but nothing helps.
What am i doing wrong?
Apps resources looks like this:
<Application.Resources>
<Color x:Key="backColor" R="255" G="0" B="255"/>
</Application.Resources>
Troubled part of MainWindow:
<Window.Resources>
<SolidColorBrush x:Key="backBrush" Color="{DynamicResource backColor}"/>
</Window.Resources>
<Grid
Name="MainGrid"
Background="{DynamicResource backBrush}">
Code, corresponding to change in color:
this.Resources.Remove("backBrush");
this.Resources.Add("backBrush",
new SolidColorBrush { Color =
(Color)Application.Current.Resources["backColor"] });
MainGrid.SetResourceReference(Grid.BackgroundProperty, "backBrush");
I also tried set color like this:
Color newColor = (Color)Application.Current.Resources["backColor"];
Application.Current.Resources["backBrush"] = new SolidColorBrush { Color = newColor };
Upvotes: 2
Views: 1091
Reputation: 128060
You forgot to set the Color's alpha value, which is zero by default:
<Application.Resources>
<Color x:Key="backColor" R="255" G="0" B="255" A="255"/>
</Application.Resources>
Now you simply change the dynamic Brush resource by
Resources["backBrush"] = new SolidColorBrush(
(Color)Application.Current.Resources["backColor"]);
Upvotes: 2