如何设置从代码应用了样式的按钮的背景颜色?
所以我有一个应用了样式的按钮。该按钮的代码是这样的:
<Button Command="w:Command.ShowTBDisplay" Style="{DynamicResource button_displayOptions}" Content="Display TextBlock" Height="34.5" Margin="0,4,0,0" Name="btn_displayTB" />
单击按钮时,我想更改其背景颜色(永久更改,直到单击不同的按钮)。我想使用的颜色是动态资源。
在 ShowTBDisplay_Executed 方法中,我有这样的代码:
btn_displayTB.Background = FindResource("cClickColor") as Brush;
但是当我这样做时,什么也没有发生,没有设置颜色。
我认为样式的颜色会覆盖我尝试将其更改为的颜色......但我不确定。
那么我该如何更改应用了样式的按钮的背景颜色呢?
这是按钮的样式,以及我希望使用的颜色(两种样式都在 app.xaml 文件中):
<Style x:Key="button_displayOptions" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Rectangle x:Name="rectangle" Fill="{DynamicResource cMenuBackground}"/>
<ContentPresenter x:Name="tb" TextBlock.Foreground="White" HorizontalAlignment="Left" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" Margin="8,5,0,5" Height="19.5"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="True"/>
<Trigger Property="IsDefaulted" Value="True"/>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Fill" TargetName="rectangle" Value="{DynamicResource cMenuItemHighlight}"/>
<Setter Property="TextBlock.Foreground" TargetName="tb" Value="Black"/>
</Trigger>
<Trigger Property="IsPressed" Value="True"/>
<Trigger Property="IsEnabled" Value="False"/>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<SolidColorBrush x:Key="cMenuItemHighlight">#FFAFAFAF</SolidColorBrush>
谢谢!
So I have a button which has a style applied to it. The code for the button is this:
<Button Command="w:Command.ShowTBDisplay" Style="{DynamicResource button_displayOptions}" Content="Display TextBlock" Height="34.5" Margin="0,4,0,0" Name="btn_displayTB" />
When the button is clicked, I want to change its background color (permanently, until a different button is clicked). The color that I want to use is a dynamicResource..
Inside the ShowTBDisplay_Executed method, I have code like this:
btn_displayTB.Background = FindResource("cClickColor") as Brush;
But when I do this, nothing happens, no color is set.
I figure that the Style's color overwrites what ever I try to change it to...but I am not sure.
So how do I go about changing the backColor of a button with a style applied to it.
Here is the style of the button, as well as the color that I wish to use (both styles are in the app.xaml file):
<Style x:Key="button_displayOptions" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Rectangle x:Name="rectangle" Fill="{DynamicResource cMenuBackground}"/>
<ContentPresenter x:Name="tb" TextBlock.Foreground="White" HorizontalAlignment="Left" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" Margin="8,5,0,5" Height="19.5"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="True"/>
<Trigger Property="IsDefaulted" Value="True"/>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Fill" TargetName="rectangle" Value="{DynamicResource cMenuItemHighlight}"/>
<Setter Property="TextBlock.Foreground" TargetName="tb" Value="Black"/>
</Trigger>
<Trigger Property="IsPressed" Value="True"/>
<Trigger Property="IsEnabled" Value="False"/>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<SolidColorBrush x:Key="cMenuItemHighlight">#FFAFAFAF</SolidColorBrush>
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的模板会覆盖默认模板,并且 Button 的
Background
属性不再执行任何操作,您需要使用TemplateBinding
将模板内的某些控件绑定到该属性。编辑:矩形不应在模板中具有具体的填充值,这可能就是您想要的:
注意新的
Setter
,它设置背景
到您的资源和Rectangle.Fill
上的内部TemplateBinding
。Your template overrides the default one and the
Background
property of the Button is no longer doing anything, you need to bind some control inside the template to the property using aTemplateBinding
for example.Edit: The rectangle should not have a concrete fill value in the template, this is probably what you want:
Note the new
Setter
which sets theBackground
to your resource and the internalTemplateBinding
on theRectangle.Fill
.