I created a button with a style, but after creating a button style, it loses the default effect of the button, and when I directly add an attribute to the button, I get these effects by default, for example, when I click, I can see a blue background. I also try Visual Manager, but it does not work. Please someone can help me find out what I'm doing wrong.
My button style:
<Style TargetType="Button" x:Key="MenuButtonStyle"> <Setter Property="HorizontalAlignment" Value="Stretch"/> <Setter Property="Foreground" Value="Black"/> <Setter Property="FontFamily" Value="Sitka Heading"/> <Setter Property="FontSize" Value="20"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Grid> <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="CommonStates"> <VisualState x:Name="Normal"/> <VisualState x:Name="Pressed"> <Storyboard> <ColorAnimation Duration="0" Storyboard.TargetName="ButtonTextElement" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" To="Blue"/> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="normalImage"> <DiscreteObjectKeyFrame KeyTime="0"> <DiscreteObjectKeyFrame.Value> <Visibility>Collapsed</Visibility> </DiscreteObjectKeyFrame.Value> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="mouseOverImage"> <DiscreteObjectKeyFrame KeyTime="0"> <DiscreteObjectKeyFrame.Value> <Visibility>Visible</Visibility> </DiscreteObjectKeyFrame.Value> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> <VisualState x:Name="MouseOver"> <Storyboard> <ColorAnimation Duration="0" Storyboard.TargetName="ButtonTextElement" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" To="Blue"/> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="normalImage"> <DiscreteObjectKeyFrame KeyTime="0"> <DiscreteObjectKeyFrame.Value> <Visibility>Collapsed</Visibility> </DiscreteObjectKeyFrame.Value> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="mouseOverImage"> <DiscreteObjectKeyFrame KeyTime="0"> <DiscreteObjectKeyFrame.Value> <Visibility>Visible</Visibility> </DiscreteObjectKeyFrame.Value> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <Border BorderBrush="Black" BorderThickness="0,0,0,0.5" Margin="30,0,0,0" Grid.ColumnSpan="2"/> <TextBlock x:Name="ButtonTextElement" Text="{TemplateBinding Content}" Margin="30,0" Foreground="{TemplateBinding Foreground}" Grid.Column="0" VerticalAlignment="{TemplateBinding VerticalAlignment}" /> <Image x:Name="normalImage" Source="/Assets/menu-arrow-left.png" Grid.Column="1" Stretch="None" HorizontalAlignment="Right" Margin="0,0,30,0" /> <Image x:Name="mouseOverImage" Source="/Assets/menu-arrow-left-hover.png" Grid.Column="1" Stretch="None" HorizontalAlignment="Right" Visibility="Collapsed" Margin="0,0,30,0" /> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>
I also change in VisualStateManager how it is
<ColorAnimation Storyboard.TargetName="MouseOverVisualElement" Storyboard.TargetProperty="TextBlock.Foreground" To="Red" />
My Button Tag
<Button Style="{StaticResource MenuButtonStyle}" Content="Home"/>
source share