如何更改按钮的背景颜色

3
无论我在按钮中更改背景属性为什么颜色,它都几乎没有变化。如果我想要一个完全不同颜色的按钮,该怎么办?
我知道可以通过编辑按钮模板来实现这一点,但这似乎有些过度。

你应该在这三个答案中选择一个来接受,这样其他的SO用户也可以从你的经验中受益,就像我一样,虽然我不得不阅读所有的答案,因为没有标记。无论如何,感谢你的问题,这帮助我解决了我的项目方案。=) - Will Marcouiller
3个回答

3

这似乎是 SilverLight 2.0 和默认的 Button ContentTemplate 的问题。我查看了源代码:

<ControlTemplate TargetType="controls:Button"> 
<Grid>
    <!-- snipped the 36 lines of VisualStatManager here -->
    <Border x:Name="Background" CornerRadius="3" Background="White" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}">
        <Grid Background="{TemplateBinding Background}"  Margin="1"> 
            <Border Opacity="0"  x:Name="BackgroundAnimation" Background="#FF448DCA" />
            <Rectangle x:Name="BackgroundGradient" >
                <Rectangle.Fill> 
                    <LinearGradientBrush StartPoint=".7,0" EndPoint=".7,1">
                        <GradientStop Color="#FFFFFFFF" Offset="0" />
                        <GradientStop Color="#F9FFFFFF" Offset="0.375" /> 
                        <GradientStop Color="#E5FFFFFF" Offset="0.625" /> 
                        <GradientStop Color="#C6FFFFFF" Offset="1" />
                    </LinearGradientBrush> 
                </Rectangle.Fill>
            </Rectangle>
        </Grid> 
    </Border>
    <ContentPresenter
            x:Name="contentPresenter" 
            Content="{TemplateBinding Content}" 
            ContentTemplate="{TemplateBinding ContentTemplate}"
            VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
            Margin="{TemplateBinding Padding}"/>
    <Rectangle x:Name="DisabledVisualElement" RadiusX="3" RadiusY="3" Fill="#FFFFFFFF" Opacity="0" IsHitTestVisible="false" /> 
    <Rectangle x:Name="FocusVisualElement" RadiusX="2" RadiusY="2" Margin="1" Stroke="#FF6DBDD1" StrokeThickness="1" Opacity="0" IsHitTestVisible="false" />
</Grid>

问题在于具有自己背景的矩形位于设置了背景的网格上方。(此外,边框是硬编码为白色的)。我们可以使用自己的ContentTemplate来解决这个问题,但这意味着还要添加所有VisualStateManager的内容,以获取与按钮一起使用的所有动画。
第二种方法是子类化Button,并在OnApplyTemplate重写中修改模板。以下是一个示例,它降低了原始Button的不透明度,使背景透过显示:
public class BKButton : Button {

public override void OnApplyTemplate() {
    base.OnApplyTemplate();

    Border border = GetTemplateChild("Background") as Border;
    Rectangle rect = GetTemplateChild("BackgroundGradient") as Rectangle;

    if(border != null) {
        border.Background = this.Background;
        border.Opacity = .6;
    }
    if (rect != null) {
        LinearGradientBrush lbrush = rect.Fill as LinearGradientBrush;
        if (lbrush != null) {
            lbrush.Opacity = .6;
        }
    }
}

ViNull,你的解决方案非常完美!现在我能够更改按钮的背景颜色了。我现在面临的唯一问题是,颜色只有在鼠标从按钮上移开后才会更改。我希望即使鼠标悬停在按钮上,也能更改颜色。你能提供一个解决方法吗? 提前致谢。 - Manoj Attal
你需要重写VisualStateManager的鼠标状态-如果你需要更多自定义更改,而不仅仅是调整背景,那么提供自己的ControlTemplate是值得的。 - ViNull

2
我不太确定你所说的“按钮几乎不变”意味着什么,但当我写下<Button x:Name="button" Background="Gold" Foreground="Red" Content="Hello!" />时,我得到了一个金色背景和红色文本的按钮。如果你想进一步编辑按钮(例如更改点击时的外观等),那么你仍然需要使用模板和视觉状态管理器。我找到了两个非常好的教程,专门关注Silverlight按钮的视觉方面:http://mattberseth.com/blog/2008/03/creating_a_custom_skin_for_sil.htmlhttp://weblogs.asp.net/dwahlin/archive/2008/11/23/using-the-visual-state-manager-in-silverlight-templates.aspx

1

我曾经遇到过同样的问题,但我找到了解决方法。我没有改变按钮的背景颜色,而是将按钮的边框宽度设置为100,然后设置边框的颜色,例如灰色。

这样你就会得到一个灰色的按钮。我知道这不是最好的方法,但对我来说有效。

以下是代码示例:

    Dim btn As New Button
    btn.Width = 75
    btn.Height = 35
    btn.BorderThickness = New Thickness(0, 0, 0, 100)
    btn.BorderBrush = New SolidColorBrush(Colors.LightGray)
    AddHandler btn.Click, AddressOf btn_Click
    AddHandler btn.MouseEnter, AddressOf btn_MouseEnter
    AddHandler btn.MouseLeave, AddressOf btn_MouseLeave

    LayoutRoot.Children.Add(btn)


Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    MessageBox.Show("pressed")
End Sub
Private Sub btn_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs)
    DirectCast(sender, Button).BorderBrush = New SolidColorBrush(Colors.Gray)
End Sub
Private Sub btn_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs)
    DirectCast(sender, Button).BorderBrush = New SolidColorBrush(Colors.LightGray)
End Sub

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接