【问题标题】:Bind color of SolidColorBrush to background color of Button将 SolidColorBrush 的颜色绑定到 Button 的背景颜色
【发布时间】:2018-05-21 06:09:47
【问题描述】:

我使用以下代码创建了一个自定义圆形按钮但是颜色按钮不遵循按钮的背景属性,所以我必须为按钮的每种颜色创建新样式。

如何将 SolidColorBrush 颜色绑定到按钮背景颜色?

        <Style x:Key="RoundedButton" TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid>
                        <Rectangle RadiusX="6" RadiusY="6">
                            <Rectangle.Fill>
                                <SolidColorBrush Color="BUTTON BACKGROUND"/>
                            </Rectangle.Fill>
                        </Rectangle>
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    按钮的背景已经是一个画笔。无需再创建一个:

    <ControlTemplate TargetType="Button">
        <Grid>
            <Rectangle RadiusX="6" RadiusY="6" Fill="{TemplateBinding Background}"/>
            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
        </Grid>
    </ControlTemplate>
    

    【讨论】:

      【解决方案2】:

      你可能不得不使用一种方式DataBinding

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-15
        • 1970-01-01
        • 2020-05-22
        • 1970-01-01
        • 1970-01-01
        • 2015-03-07
        • 2016-01-23
        • 1970-01-01
        相关资源
        最近更新 更多