最后我得到了我的自定义按钮,几乎就像我想要它一样。
问题是代码:D(IsChecked
是a DependencyProperty
)
<ControlTemplate.Triggers>
<!-- EventTriggers for LMBUp/LMBDown/MouseEnter/MouseLeave -->
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="True"/>
<Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsChecked}" Value="False"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource MouseEnter}"/>
</MultiDataTrigger.EnterActions>
<MultiDataTrigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource MouseLeave}"/>
</MultiDataTrigger.ExitActions>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="True"/>
<Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsChecked}" Value="True"/>
</MultiDataTrigger.Conditions>
<Setter TargetName="LayoutRoot" Property="Background" Value="Red"/>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="False"/>
<Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsChecked}" Value="True"/>
</MultiDataTrigger.Conditions>
<Setter TargetName="LayoutRoot" Property="Background" Value="Red"/>
</MultiDataTrigger>
</ControlTemplate.Triggers>
我认为它有点“肮脏”,并且没有完全像预期的那样。每次用户单击按钮IsChecked
时,都会从 切换true
到false
,反之亦然。现在如果发生以下情况
- IsChecked == 真
- 用户单击将 IsChecked 切换为 false
- 鼠标一直在按钮上
那么我希望按钮变成 $MouseOverColor$ 而不是 $ButtonNormalBackground$ 因为第一个MultiDataTrigger
.
我的问题是是否可以简化代码并纠正最后一个问题?
提前致谢!