【发布时间】:2013-09-04 20:35:01
【问题描述】:
我想将我的按钮重新设计为更扁平,所以我创建了两种样式;第一个是从ToolBar.ToggleButtonStyleKey 派生的ToggleButton,第二个是从ToolBar.ButtonStyleKey 派生的Button。
效果很好 - 按钮现在类似于工具栏且扁平。我想要的第二件事是让Background 属性在用户将光标悬停在控件上时透明。因此,为了实现这一点,我定义了一个简单的触发器,在 IsMouseOver 事件上将 Background 设置为 Transparent。它适用于ToggleButton,但是,相同的触发器不适用于Button(背景完全不受影响)。
有谁知道为什么这个触发器对ToggleButton 有效而对Button 无效?我确实期待相同的行为,因为两种样式都来自同一个家族。
以下是完整代码。
<Window x:Class="WpfButtonsTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="FlatToggleButton" TargetType="ToggleButton"
BasedOn="{StaticResource {x:Static ToolBar.ToggleButtonStyleKey}}">
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Focusable" Value="False"/>
<Setter Property="Background" Value="Transparent"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Transparent"/>
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="FlatButton" TargetType="Button"
BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}">
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Focusable" Value="False"/>
<Setter Property="Background" Value="Transparent"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Transparent"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<StackPanel>
<ToggleButton Style="{StaticResource FlatToggleButton}">
I am Toggle Button
</ToggleButton>
<Button Style="{StaticResource FlatButton}">
I am Button
</Button>
</StackPanel>
</Grid>
</Window>
【问题讨论】: