Reputation: 45
I've got a WPF button with a ControlTemplate including a trigger on the IsPressed property (snippet shown below). The button is a "press and hold" type of button, so while the button is pressed down I am trying to change its color to provide some visual feedback.
The trigger shown works great. When you click down on the button the color changes, and remains changed until you release the mouse - EXCEPT when the mouse is moved out of the button while still pressed down. In that case, the color changes back to the default, even though the button is still being pressed down and the mechanism the button is driving continues until the mouse is released.
Has anyone else seen this issue or know of a solution? This is my first experience with WPF triggers so any help would be greatly appreciated.
Thanks everyone.
Here's the style I'm currently applying to the button (with some irrelevant stuff removed):
<Style x:Key="RS_LocalButtonStyle"
TargetType="Button">
<Setter Property="Margin"
Value="0,7,0,0" />
<Setter Property="HorizontalAlignment"
Value="Stretch" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ButtonBase}">
<Border x:Name="border"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="True">
<ContentPresenter x:Name="contentPresenter"
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"
ContentStringFormat="{TemplateBinding ContentStringFormat}"
Focusable="False"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Button.IsDefaulted"
Value="True">
<Setter Property="BorderBrush"
TargetName="border"
Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
</Trigger>
<Trigger Property="ToggleButton.IsChecked"
Value="True">
<Setter Property="Background"
TargetName="border"
Value="#FFBCDDEE" />
</Trigger>
<Trigger Property="IsEnabled"
Value="False">
<Setter Property="Background"
TargetName="border"
Value="#FFF4F4F4" />
</Trigger>
<Trigger Property="IsMouseOver"
Value="True">
<Setter Property="Background"
TargetName="border"
Value="#FFBEE6FD" />
</Trigger>
<Trigger Property="IsPressed"
Value="True">
<Setter Property="Background"
TargetName="border"
Value="{DynamicResource {x:Static SystemColors.GradientActiveCaptionBrushKey}}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Upvotes: 0
Views: 727
Reputation: 169400
The thing is that the IsPressed
property is set to false
when you move the mouse of the button, but you could replace your IsPressed
trigger with the following MultiTrigger
to achieve what you want:
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True"/>
<Condition Property="IsMouseCaptured" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="Background"
TargetName="border"
Value="{DynamicResource {x:Static SystemColors.GradientActiveCaptionBrushKey}}"/>
</MultiTrigger>
Upvotes: 1