Goal To make the default behavior for a buttons background onMouseEnter be to do nothing, yet still allow for programatically changing the button background in the logic of the application.
Multiple posts address overriding the default MouseOver behaviour:
ex: How do you disable MouseOver effects on a Button in WPF?
However, in my application I need to keep changing the background color, and setting the MouseOver background color as a Trigger overrides my changes.
<Button Content="Start" Name="buttonStart" Style="{StaticResource mainButton}" PreviewMouseDown="buttonStart_MouseDown"/>
<Button Content="Stop" Name="buttonStop" Style="{StaticResource mainButton}" PreviewMouseDown="buttonStop_MouseDown"/>
Style in App.xaml
<Style x:Key="mainButton" TargetType="{x:Type Button}">
<Setter Property="Width" Value="250"/>
<Setter Property="Height" Value="75"/>
<Setter Property="FontSize" Value="40"/>
<Setter Property="FontFamily" Value="Segoe UI Light"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Name="Border" BorderThickness="1"
BorderBrush="#404040">
<ContentPresenter Margin="2"
HorizontalAlignment="Center"
VerticalAlignment="Center"
RecognizesAccessKey="True"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="Border"
Property="Background" Value="{x:Null}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Logic:
private void buttonStop_MouseDown(object sender, RoutedEventArgs e)
{
//Change background colors for buttonStop and buttonStart
}
private void buttonStart_MouseDown(object sender, RoutedEventArgs e)
{
//Change background colors for buttonStop and buttonStart
}
How do I make the background on buttons do nothing onMouseEnter, yet still change the background in the code?