如何修改UWP中Button控件的显示效果颜色?

针对UWP Button控件的灰色高亮颜色替换,有两种常用实现方式:
1. 重写系统资源快速修改
UWP Button的高亮颜色默认绑定系统资源SystemControlHighlightAccentBrush,你可以通过重写该资源来替换颜色:
全局生效(所有Button)
在App.xaml的<ResourceDictionary>中添加:
<Application.Resources> <ResourceDictionary> <SolidColorBrush x:Key="SystemControlHighlightAccentBrush" Color="#FF0078D7"/> <!-- 替换为目标颜色 --> </ResourceDictionary> </Application.Resources>
单个Button生效
直接在目标Button的资源块中定义:
<Button Content="测试按钮"> <Button.Resources> <SolidColorBrush x:Key="SystemControlHighlightAccentBrush" Color="#FF2ECC71"/> </Button.Resources> </Button>
2. 自定义ControlTemplate精细控制状态
如果需要区分悬停、按压等不同交互状态的高亮颜色,可自定义Button的模板:
<Button Content="自定义高亮按钮"> <Button.Template> <ControlTemplate TargetType="Button"> <Grid x:Name="RootGrid" Background="{TemplateBinding Background}"> <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="CommonStates"> <VisualState x:Name="Normal"/> <!-- 鼠标悬停状态 --> <VisualState x:Name="PointerOver"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid" Storyboard.TargetProperty="Background"> <DiscreteObjectKeyFrame KeyTime="0" Value="#FF3498DB"/> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> <!-- 按钮按压状态 --> <VisualState x:Name="Pressed"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid" Storyboard.TargetProperty="Background"> <DiscreteObjectKeyFrame KeyTime="0" Value="#FF2980B9"/> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <ContentPresenter x:Name="ContentPresenter" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Margin="{TemplateBinding Padding}"/> </Grid> </ControlTemplate> </Button.Template> </Button>
你可以根据需求修改PointerOver和Pressed状态对应的Color值,实现不同交互场景下的高亮效果。
内容的提问来源于stack exchange,提问作者Ali Teleb
相关产品推荐
相关产品推荐

