Reputation: 311
I have a label in my UI and have a style for it.
<Style x:Key="ColorChangingLabel" TargetType="{x:Type Label}">
<Setter Property="FontSize" Value="13"></Setter>
<Setter Property="Foreground" Value="#ff676767"></Setter>
<Setter Property="Background" Value="White"></Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#ffEAECEE"></Setter>
<Setter Property="Foreground" Value="#ff0067B0"></Setter>
</Trigger>
</Style.Triggers>
</Style>
Besides MouseOver
trigger i also want to set the same property values when the label is clicked(as click event is not available for label i use MouseDown event).
Here is MouseDown
event code
private void myLabel_MouseDown(object sender, MouseButtonEventArgs e){
myLabel.Foreground = new SolidColorBrush(Color.FromArgb(0xff, 0x00, 0x67, 0xB0));
myLabel.Background = new SolidColorBrush(Color.FromArgb(0xff, 0xea, 0xec, 0xee));
}
So my problem is if i mouse down on my label, after it my MouseOver style is never applied to my label. I know about style overriding, but don't know how to get rid of this.
Upvotes: 0
Views: 718
Reputation: 25623
Your MouseDown
handler is setting a local value for the Foreground
and Background
, and local values take precedence over all other value sources, including those set by triggers and setters. See Dependency Property Value Precedence on MSDN for more information.
Use SetCurrentValue
to change the current property value without switching to a local value:
myLabel.SetCurrentValue(
Control.ForegroundProperty,
SolidColorBrush(Color.FromArgb(0xff, 0x00, 0x67, 0xB0)));
Alternatively, apply a custom style to myLabel
that uses triggers to change the Foreground
and Background
, and get rid of the event handler altogether.
Upvotes: 1