WPF: Change background color of a template
I have defined a menu template which, in theory, should use the backgroundcolor of the "AppBackground" SoliColorBrush, defined in a separate file.
When in "debug mode", I use the following code to change that variable: this.Resources["AppBackground"] = new SolidColorBrush(Colors.DarkGreen);
This has the intended effect on the application background, however, it doesn't seem to have an effect on the custom menu design I have. I have tried to use both StaticResource and DynamicResource, without any luck. Is this a known issue, and is there a trick here?
Defined in R开发者_运维知识库esourceDirectory:
<SolidColorBrush x:Key="AppBackground" Color="#003466"/>
Defined in a file:
<Style x:Key="{x:Type MenuItem}" TargetType="{x:Type MenuItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MenuItem}">
<Border x:Name="Border" BorderThickness="1">
<Grid Background="{Binding Source={StaticResource AppBackground}}">
Havent seen that you marked that it works also not with DynamicBinding. I only looked at the code. Thats why I wrote my first answer:
Use DynamicBinding, that should work.
The real problem you wrapp the resource through a Binding. Remove this, as I wrote in my other post. Sorry the chaos with the multiple answers.
<Grid Background="{DynamicResource AppBackground}">
It seems to be a typing error: Remove the {Binding...
and it works:
<Grid Background="{DynamicResource AppBackground}">
The binding acts as a wrapper between the ressource and the destination and prevents the tracking of the changes.
精彩评论