WPF,这个触发器为什么无效?

F35rtt 2015-04-09 09:19:06

<Window x:Class="WPF2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF2"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="ButtonNextStyle" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Name="PART_Border" Background="#FFDC5555" Width="100" Height="70"/>
<ControlTemplate.Triggers>
<Trigger Property="Button.IsPressed" Value="true">
<Setter TargetName="PART_Border" Property="Background" Value="blue"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="PART_Border" Property="Background" Value="#FF57E02F"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Button Style="{StaticResource ButtonNextStyle}"/>
</Grid>
</Window>


在上面的代码中,Button样式中,控件模板中有两个触发器,一个是IsPressed,一个是IsMouseOver。可是,当鼠标按下Button之后,背景色并没有变成蓝色。
如果取消IsMouseOver触发器,按下Button背景色就会变成蓝色,这是什么原因呢?
...全文
216 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Forty2 2015-04-10
  • 打赏
  • 举报
回复
实际上两个事件都触发了,只是后面的设置取代了前面的设置。 你把次序对调一下就可以了:
<ControlTemplate.Triggers>
    <Trigger Property="IsMouseOver" Value="true">
        <Setter TargetName="PART_Border" Property="Background" Value="#FF57E02F"/>
    </Trigger>
    <Trigger Property="Button.IsPressed" Value="true">
        <Setter TargetName="PART_Border" Property="Background" Value="blue"/>
    </Trigger>
</ControlTemplate.Triggers>
F35rtt 2015-04-09
  • 打赏
  • 举报
回复
引用 1 楼 wjq 的回复:
Button.IsPressed换成IsPressed
不是这个原因
wjq 2015-04-09
  • 打赏
  • 举报
回复
Button.IsPressed换成IsPressed

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧