如何保留自定义 WPF RepeatButton 的默认悬停效果
当我创建自定义 WPF 控件时,我通常会保留通常的默认外观和效果。例如,这包括按钮上默认的蓝色悬停。
我创建了一个带有可滚动选项卡的自定义 TabControl。不管出于什么原因,每一侧的RepeatButton(即左右滚动箭头)都没有悬停效果。我找到了几个教程,告诉我如何为控件创建自己的悬停效果,但不幸的是,这些示例只是用不同的颜色替换背景(这看起来与在保持底层背景的同时淡入透明蓝色的动画非常不同) )。我怎样才能获得这种悬停效果?有什么方法可以访问标准悬停效果(可能是故事板吗?)?
编辑:我刚刚发现,如果删除放在重复按钮上的自定义样式,我会得到默认的悬停效果。也许问题是——我如何破坏悬停效果,以及如何在不破坏它的情况下温和地修改外观?下面是破坏悬停效果的样式-
<Style x:Key="TabScrollerRepeatButtonStyle" TargetType="{x:Type RepeatButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border x:Name="testTest" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1" Margin="1,0" CornerRadius="2">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Content="{TemplateBinding ContentControl.Content}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
When I create a custom WPF control, I usually retain the usual default appearance and effects. For example, this includes the default blue hover over a button.
I have created a custom TabControl with scrollable tabs. For whatever reason, the RepeatButton on each side (i.e. the left and right scroll arrows) have no hover effect. I have found several tutorials that tell me how to create my own hover effect for controls, but unfortunately these examples just replace the background with a different color (and this looks very different than having an animation which fades in transparent blue while keeping the underlying background). How can I get this hover effect? Is there some way that I can access the standard hover effect (might it be a storyboard?)?
Edit: I have just discovered that if I remove the custom Style that I put on the RepeatButton, I get the default hover effect. Perhaps the question is then -- How did I destroy the hover effect, and how do I mildly modify appearance without destroying it? Below is the Style which borked the hover effect-
<Style x:Key="TabScrollerRepeatButtonStyle" TargetType="{x:Type RepeatButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border x:Name="testTest" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1" Margin="1,0" CornerRadius="2">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Content="{TemplateBinding ContentControl.Content}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决此类问题的最佳方法是启动 Expression Blend 并为要从中复制样式的控件执行“编辑模板”。对滚动条执行此操作将准确显示默认样式的作用,然后您可以将相关位复制到自定义控件样式。
The best way to attack problems like these is to fire up Expression Blend and do 'Edit Template' for the control you want to copy styles from. Doing this for the scrollbar will show you exactly what the default style does and you can then copy relevant bits to your custom control style.