Reputation: 2613
I have 2 styles for a button. The first one is for normal state and the second is for mouseOver state. How can I change the button style when the mouse enters it? In Blend I tried to create a storyboard and change the style but nothing happened.
Upvotes: 1
Views: 476
Reputation: 84647
You could bind the Style
property to IsMouseOver
and use a generic "true value"/"false value" converter.
You can specify the converter like this
<Window.Resources>
<Style TargetType="Button" x:Key="normalStyle">
<Setter Property="Foreground" Value="Green"/>
</Style>
<Style TargetType="Button" x:Key="mouseOverStyle">
<Setter Property="Foreground" Value="Red"/>
</Style>
<converters:BooleanObjectConverter FalseValue="{StaticResource normalStyle}"
TrueValue="{StaticResource mouseOverStyle}"
x:Key="styleConverter"/>
</Window.Resources>
And then bind Style
to IsMouseOver
<Button Style="{Binding RelativeSource={RelativeSource Self},
Path=IsMouseOver,
Converter={StaticResource styleConverter}}"
... />
BooleanObjectConverter
public class BooleanObjectConverter : IValueConverter
{
public object TrueValue { get; set; }
public object FalseValue { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((bool)value == true)
{
return TrueValue;
}
return FalseValue;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
Upvotes: 3