Essentially, we have a ToggleButton with custom styling that includes different colors for the unchecked, checked, and mouse hover states. The ToggleButton correctly displays the color for the checked state after being clicked. However, when the mouse hovers over the ToggleButton while it's in the checked state, the color changes as expected, but once the mouse is moved away, the color reverts back to that of the unchecked state, even though the ToggleButton remains in the checked state.
This is my custom style code:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="ToggleButton" x:Key="ToggleButtonTheme">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton">
<Grid>
<Grid.Resources>
<Storyboard x:Key="ToggleOnAnimation">
<DoubleAnimation Storyboard.TargetName="SlideButton"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)"
To="12" Duration="0:0:0.1"/>
</Storyboard>
<Storyboard x:Key="ToggleOffAnimation">
<DoubleAnimation Storyboard.TargetName="SlideButton"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)"
To="-12" Duration="0:0:0.1"/>
</Storyboard>
</Grid.Resources>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="Disabled"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<ColorAnimation Storyboard.TargetName="Border"
Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
To="#848383" Duration="0:0:0.1"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="CheckStates">
<VisualState x:Name="Unchecked">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="SlideButton"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)"
To="-12" Duration="0:0:0.1"/>
<ColorAnimation Storyboard.TargetName="Border"
Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
To="#a3a2a2" Duration="0:0:0.2"/>
</Storyboard>
</VisualState>
<VisualState x:Name="Checked">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="SlideButton"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)"
To="12" Duration="0:0:0.1"/>
<ColorAnimation Storyboard.TargetName="Border"
Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
To="#474646" Duration="0:0:0.2"/>
</Storyboard>
</VisualState>
<VisualState x:Name="CheckedMouseOver">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="SlideButton"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)"
To="12" Duration="0:0:0.1"/>
<ColorAnimation Storyboard.TargetName="Border"
Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
To="#848383" Duration="0:0:0.2"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="Border"
Background="#a3a2a2"
CornerRadius="12"
BorderThickness="0">
<Grid>
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="5"/>
<Border x:Name="SlideButton"
Background="White"
CornerRadius="13"
BorderThickness="0"
Width="16" Height="16">
<Border.RenderTransform>
<TranslateTransform X="0"/>
</Border.RenderTransform>
</Border>
</Grid>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I would be very grateful if somebody could help me with this matter as I am not yet very familiar with custom button styles