WPF Button Setter Template Setter.Value ControlTemplate Border ContentPresenter ControlTemplate.Triggers

FredGrit發表於2024-11-03
<StackPanel.Resources>
    <Style x:Key="btnStyle" TargetType="{x:Type Button}">
        <Setter Property="Button.FontSize" Value="50"/>
        <Setter Property="Button.Background" Value="Purple"/>
        <Setter Property="Button.Foreground" Value="Cyan"/>
        <Setter Property="Button.Height" Value="300"/>
        <Setter Property="Button.Width" Value="500"/>
        <Setter Property="Button.RenderTransformOrigin" Value="0.5,0.5"/>
        <Setter Property="Button.RenderTransform">
            <Setter.Value>
                <RotateTransform Angle="10"/>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border x:Name="border" 
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Padding="{TemplateBinding Padding}">
                        <ContentPresenter HorizontalAlignment="Center"
                                          VerticalAlignment="Center"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="border" Property="Background" Value="Red"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="btnBasedStyle" BasedOn="{StaticResource btnStyle}" TargetType="{x:Type Button}">
        <Setter Property="Button.FontWeight" Value="Bold"/>
        <Setter Property="FontSize" Value="150"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border
                        x:Name="border"
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                        <ContentPresenter 
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="border" Property="Background" Value="Yellow"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</StackPanel.Resources>

相關文章