1 回答
TA贡献1796条经验 获得超4个赞
依赖属性概述:
绑定被视为本地值,这意味着如果您设置另一个本地值,您将消除绑定。有关详细信息,请参阅依赖属性值优先级。
如果您对该Click
事件感兴趣,那么您应该使用EventTrigger
(或)使用 XAMLVisualStateManager
来操作状态:Button
<Style x:Key="MainMenuStyle"
TargetType="{x:Type Button}">
<Setter Property="Background"
Value="#FF303136" />
<Setter Property="Foreground"
Value="#FF6F7074" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="ButtonBorder" Background="{TemplateBinding Background}"
Padding="30, 0, 0, 0">
<ContentPresenter HorizontalAlignment="Left"
VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="Click">
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="ButtonBorder"
Storyboard.TargetProperty="Background.(SolidColorBrush.Color)"
Duration="0:0:0"
To="Coral" />
<ColorAnimation Storyboard.Target="{Binding RelativeSource={RelativeSource TemplatedParent}}"
Storyboard.TargetProperty="Foreground.(SolidColorBrush.Color)"
Duration="0:0:0"
To="White" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
但如果您只想提供按钮单击反馈,您应该在该Button.IsPressed属性上触发:
<Style x:Key="MainMenuStyle"
TargetType="{x:Type Button}">
<Setter Property="Background"
Value="#FF303136" />
<Setter Property="Foreground"
Value="#FF6F7074" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="ButtonBorder" Background="{TemplateBinding Background}"
Padding="30, 0, 0, 0">
<ContentPresenter HorizontalAlignment="Left"
VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background"
Value="Coral" />
<Setter Property="Foreground"
Value="White" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
编辑
背景色之所以被设置回来,是因为触发器的原因IsMouseOver。您所描述的从Button.Click事件处理程序切换样式的解决方案并不干净,无法回答您的问题。
您希望Button单击(选择)时更改颜色,同时应用鼠标悬停效果。
因此我建议替换Button为ToggleButton. ToggleButton提供两种开箱即用的状态(选中和未选中):
<Style x:Key="MainMenuStyle"
TargetType="{x:Type ToggleButton}">
<Setter Property="Background"
Value="#FF303136" />
<Setter Property="Foreground"
Value="#FF6F7074" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border x:Name="ButtonBorder" Background="{TemplateBinding Background}"
Padding="30, 0, 0, 0">
<ContentPresenter HorizontalAlignment="Left"
VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Background"
Value="Coral" />
<Setter Property="Foreground"
Value="White" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background"
Value="#FF2A2A32" />
<Setter Property="Foreground"
Value="#FFBBBCB8" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsChecked" Value="True" />
<Condition Property="IsMouseOver" Value="True" />
</MultiTrigger.Conditions>
<Setter Property="Background"
Value="Coral" />
<Setter Property="Foreground"
Value="#FFBBBCB8" />
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
- 1 回答
- 0 关注
- 51 浏览
添加回答
举报