简体   繁体   English

WPF:更改模板的背景色

[英]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. 我已经定义了一个菜单模板,从理论上讲,它应该使用在单独文件中定义的“ AppBackground” SoliColorBrush的背景色。

When in "debug mode", I use the following code to change that variable: this.Resources["AppBackground"] = new SolidColorBrush(Colors.DarkGreen); 在“调试模式”下,我使用以下代码更改该变量: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. 我试图同时使用StaticResource和DynamicResource,但没有任何运气。 Is this a known issue, and is there a trick here? 这是一个已知问题,这里有技巧吗?

Defined in ResourceDirectory: 在ResourceDirectory中定义:

<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. Havent看到您标记了它也不适用于DynamicBinding。 I only looked at the code. 我只看了代码。 Thats why I wrote my first answer: 这就是为什么我写第一个答案的原因:

Use DynamicBinding, that should work. 使用DynamicBinding,应该可以。

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: 似乎是键入错误:删除{Binding... ,它可以工作:

<Grid Background="{DynamicResource AppBackground}"> 

The binding acts as a wrapper between the ressource and the destination and prevents the tracking of the changes. 绑定充当资源和目标之间的包装,并阻止跟踪更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM