ButtonTemplate.axaml代码

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Width="300" Height="300"
        x:Class="AvaloniaUI.ButtonTemplate"
        Title="ButtonTemplate">
    <!-- 资源区 -->
    <Window.Resources>
        <LinearGradientBrush x:Key="NormalBrush" StartPoint="0,0" EndPoint="0,1">
            <GradientStop Color="#FFF" Offset="0.0"/>
            <GradientStop Color="#F5F5F5" Offset="0.6"/>
            <GradientStop Color="#DCDCDC" Offset="1.0"/>
        </LinearGradientBrush>

        <LinearGradientBrush x:Key="DefaultedBorderBrush" StartPoint="0,0" EndPoint="0,1">
            <GradientStop Color="#777" Offset="0.0"/>
            <GradientStop Color="#000" Offset="1.0"/>
        </LinearGradientBrush>

        <LinearGradientBrush x:Key="DarkBrush" StartPoint="0,0" EndPoint="0,1">
            <GradientStop Color="#FFF" Offset="0.0"/>
            <GradientStop Color="#FDFDFD" Offset="0.4"/>
            <GradientStop Color="#F5F5F5" Offset="1.0"/>
        </LinearGradientBrush>

        <LinearGradientBrush x:Key="PressedBrush" StartPoint="0,0" EndPoint="0,1">
            <GradientStop Color="#BBB" Offset="0.0"/>
            <GradientStop Color="#EEE" Offset="0.1"/>
            <GradientStop Color="#EEE" Offset="0.9"/>
            <GradientStop Color="#FFF" Offset="1.0"/>
        </LinearGradientBrush>

        <LinearGradientBrush x:Key="PressedBorderBrush" StartPoint="0,0" EndPoint="0,1">
            <GradientStop Color="#444" Offset="0.0"/>
            <GradientStop Color="#888" Offset="1.0"/>
        </LinearGradientBrush>

        <SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#EEE"/>
        <SolidColorBrush x:Key="DisabledBorderBrush" Color="#AAA"/>
        <SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888"/>

        <LinearGradientBrush x:Key="NormalBorderBrush" StartPoint="0,0" EndPoint="0,1">
            <GradientStop Color="#CCC" Offset="0.0"/>
            <GradientStop Color="#444" Offset="1.0"/>
        </LinearGradientBrush>
    </Window.Resources>

    <!-- 样式定义 -->
    <Window.Styles>
        <Style Selector="Button.Templated">
            <Setter Property="HorizontalAlignment" Value="Stretch"/>
            <Setter Property="VerticalAlignment" Value="Stretch"/>
            <Setter Property="Template">
                <ControlTemplate>
                    <Border x:Name="border"
                            CornerRadius="2"
                            BorderThickness="1"
                            Background="{StaticResource NormalBrush}"
                            BorderBrush="{StaticResource NormalBorderBrush}">
                        <ContentPresenter Margin="{TemplateBinding Padding}"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          Content="{TemplateBinding Content}"
                                          ContentTemplate="{TemplateBinding ContentTemplate}"
                                          RecognizesAccessKey="True"/>
                    </Border>
                </ControlTemplate>
            </Setter>

            <!-- 鼠标悬停 -->
            <Style Selector="^:pointerover /template/ Border#border">
                <Setter Property="Background" Value="{StaticResource DarkBrush}"/>
            </Style>

            <!-- 按下 -->
            <Style Selector="^:pressed /template/ Border#border">
                <Setter Property="Background" Value="{StaticResource PressedBrush}"/>
                <Setter Property="BorderBrush" Value="{StaticResource PressedBorderBrush}"/>
            </Style>

            <!-- 聚焦 -->
            <Style Selector="^:focus /template/ Border#border">
                <Setter Property="BorderBrush" Value="{StaticResource DefaultedBorderBrush}"/>
            </Style>

            <!-- 禁用 -->
            <Style Selector="^:disabled /template/ Border#border">
                <Setter Property="Background" Value="{StaticResource DisabledBackgroundBrush}"/>
                <Setter Property="BorderBrush" Value="{StaticResource DisabledBorderBrush}"/>
            </Style>

            <Style Selector="^:disabled /template/ ContentPresenter">
                <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
            </Style>
        </Style>
    </Window.Styles>
    <StackPanel Margin="5">
        <Button Padding="5" Margin="3" HorizontalAlignment="Center">Normal Button</Button>
        <Button Classes="Templated" Padding="5" Margin="3">Styled Button</Button>
        <Button Classes="Templated" Padding="5" Margin="3" IsEnabled="False">Disabled Button</Button>
    </StackPanel>
</Window>

ButtonTemplate.axaml.cs代码

using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;

namespace AvaloniaUI;

public partial class ButtonTemplate : Window
{
    public ButtonTemplate()
    {
        InitializeComponent();
    }
}

运行效果

image

 

posted on 2025-11-01 09:47  dalgleish  阅读(2)  评论(0)    收藏  举报