You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MahApps Metro中Slider的RepeatButton鼠标悬停背景修改问题

解决Slider中RepeatButton鼠标悬停背景无法修改的问题

你的问题出在RepeatButton的默认模板会通过VisualState控制鼠标悬停状态的样式,仅通过Style的Setter或自定义helper属性无法覆盖默认的蓝色背景。需要自定义RepeatButton的ControlTemplate,显式定义MouseOver状态的背景颜色。

修改后的CustomRepeatButtonStyle如下:

<Style x:Key="CustomRepeatButtonStyle" TargetType="RepeatButton">
    <Setter Property="Background" Value="Orange" />
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="controls:ControlsHelper.CornerRadius" Value="0" />
    <!-- 自定义ControlTemplate,覆盖默认状态样式 -->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="RepeatButton">
                <Grid x:Name="RootGrid" Background="{TemplateBinding Background}">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <!-- 正常状态 -->
                            <VisualState x:Name="Normal" />
                            <!-- 鼠标悬停状态:设置背景为红色 -->
                            <VisualState x:Name="MouseOver">
                                <Storyboard>
                                    <ColorAnimation 
                                        Storyboard.TargetName="RootGrid"
                                        Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)"
                                        To="Red" 
                                        Duration="0:0:0" />
                                </Storyboard>
                            </VisualState>
                            <!-- 按下状态(可选) -->
                            <VisualState x:Name="Pressed">
                                <Storyboard>
                                    <ColorAnimation 
                                        Storyboard.TargetName="RootGrid"
                                        Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)"
                                        To="DarkRed" 
                                        Duration="0:0:0" />
                                </Storyboard>
                            </VisualState>
                            <!-- 禁用状态(可选) -->
                            <VisualState x:Name="Disabled">
                                <Storyboard>
                                    <ColorAnimation 
                                        Storyboard.TargetName="RootGrid"
                                        Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)"
                                        To="Gray" 
                                        Duration="0:0:0" />
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsFocused" Value="True">
            <Setter Property="Background" Value="Orange" />
        </Trigger>
    </Style.Triggers>
</Style>

说明:

  • 通过自定义ControlTemplate,我们直接在MouseOver状态的Storyboard中指定了背景色为红色,彻底覆盖默认模板的蓝色样式。
  • 保留了你原来设置的基础背景色(Orange),同时补充了按下、禁用等状态的样式(可选,可根据需求调整)。
  • 移除了无效的controls:ItemHelper相关属性,这些属性对你当前的RepeatButton样式控制没有作用。

内容的提问来源于stack exchange,提问作者markzzz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 05:20:56