繁体   English   中英

如何在xaml / UWP中覆盖嵌套的样式属性

[英]How to overwrite a nested style property in xaml/UWP

我想要一个红色按钮。 我无法获得红色按钮,并且我认为原因是我无法覆盖所有按钮默认为的样式的template属性。

该按钮本身是准系统,设置其“背景”不会更改其颜色:

<Button x:Name="redButton" Content="Red Button" Width="180" Height="80" Background="Red"/>

默认样式为:

<Style x:Key="ButtonBase" TargetType="Button">
    <Style.Setters>
        <Setter Property="Foreground" Value="{StaticResource ButtonForeground}" />
        <Setter Property="FontSize" Value="{StaticResource ButtonFontSize}" />
        <Setter Property="HorizontalContentAlignment" Value="Center" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="MinWidth" Value="80" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid x:Name="RootGrid" Width="Auto">
                        <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                            <ContentPresenter
                                x:Name="Content"
                                Margin="5"
                                HorizontalAlignment="Center"
                                VerticalAlignment="Center" />
                        </Grid>
                        <VisualStateManager.VisualStateGroups>
                            <!--                               -->
                            <!-- About a hundred lines of code -->
                            <!--                               -->
                        </VisualStateManager.VisualStateGroups>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style.Setters>
</Style>

我想直接在“ RootGrid”内部的网格具有红色背景。 获得这种风格的红色按钮的最简单方法是什么?

在ControlTemplate中为网格的Background属性设置一个TemplateBinding:

<Grid x:Name="RootGrid" Width="Auto">
    <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="{TemplateBinding Background}">

您需要按钮的覆盖模板:

    <Button Content="sample button">
        <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Background="Red" BorderBrush="DimGray" BorderThickness="1" CornerRadius="2">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                </Border>
            </ControlTemplate>
        </Button.Template>
    </Button>

暂无
暂无

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

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