AvaloniaUI 按钮悬停效果实现教程
要创建一个悬停样式的按钮,您可以按照以下步骤操作:
-
在您的 Avalonia 项目中创建一个新的样式文件,例如'ButtonStyles.axaml'。
-
在样式文件中添加一个新的样式,例如:
<Style Selector="Button">
<Setter Property="Background" Value="#2196F3" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Padding" Value="10" />
<Setter Property="Margin" Value="5" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="FontSize" Value="14" />
<Setter Property="Template">
<ControlTemplate>
<Border Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Margin="{TemplateBinding Padding}" />
</Border>
</ControlTemplate>
</Setter>
<Setter Property="Background" Value="#1976D2" Trigger="IsMouseOver=True" />
</Style>
-
在样式中,我们添加了一个'Trigger' 属性,用于在鼠标悬停在按钮上时更改按钮的颜色。这个属性会在 IsMouseOver 为 True 时触发。
-
现在,您可以在您的 Avalonia 窗口中使用这个按钮样式,例如:
<Button Content="Click me!" Style="{DynamicResource Button}" />
这将创建一个带有悬停效果的按钮,当鼠标悬停在按钮上时,按钮的背景颜色会从蓝色变为深蓝色。
原文地址: https://www.cveoy.top/t/topic/nDB9 著作权归作者所有。请勿转载和采集!