1
votes

I'm trying to edit the default template of a button for a WPF application,the "MouseEnter" and "MouseLeave" events are working,the problem is with the "IsPressed" event that basically seems to not be triggered.I also removed the

RenderPressed="{TemplateBinding IsPressed}"

part but the button just seems to ignore my IsPressed event Here's the code:

<LinearGradientBrush x:Key="ButtonNormalBackground" EndPoint="0,1" StartPoint="0,0">
    <GradientStop Color="#F3F3F3" Offset="0"/>
    <GradientStop Color="#EBEBEB" Offset="0.5"/>
    <GradientStop Color="#DDDDDD" Offset="0.5"/>
    <GradientStop Color="#CDCDCD" Offset="1"/>
</LinearGradientBrush>
<SolidColorBrush x:Key="ButtonNormalBorder" Color="#FF707070"/>
<Style x:Key="ButtonDark1" TargetType="{x:Type Button}">
    <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
    <Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
    <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Padding" Value="1"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" RenderDefaulted="{TemplateBinding IsDefaulted}" SnapsToDevicePixels="true">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Themes:ButtonChrome>
                <ControlTemplate.Triggers>
                    <EventTrigger RoutedEvent="Control.MouseEnter">
                        <BeginStoryboard>
                            <Storyboard >
                                <DoubleAnimation Duration="0:0:0.3" To="0.8" Storyboard.TargetProperty="Opacity"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                    <EventTrigger RoutedEvent="Control.MouseLeave">
                        <BeginStoryboard>
                            <Storyboard >
                                <DoubleAnimation Duration="0:0:0.3" To="0.5" Storyboard.TargetProperty="Opacity"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                    <Trigger Property="IsKeyboardFocused" Value="true">
                        <Setter Property="RenderDefaulted" TargetName="Chrome" Value="false"/>
                    </Trigger>
                    <Trigger Property="ToggleButton.IsChecked" Value="true">
                        <Setter Property="RenderPressed" TargetName="Chrome" Value="true"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="#ADADAD"/>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter Property="Foreground" Value="LightBlue"></Setter>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Thanks in advance.

1

1 Answers

1
votes

Once again, we have a question where someone has messed up their ControlTemplate and wants someone else to fix it. The answer is always the same... start with the default ControlTemplate for your Button. You can find the default ControlTemplate for your Button in the Button Styles and Templates page on MSDN.

If you look at the linked page, you'll see a VisualStateManager.VisualStateGroups collection and inside that collection, you'll find a VisualState named Pressed. A little further up, you should see this:

<VisualTransition GeneratedDuration="0" To="Pressed" />

These are what manage the visual Pressed state.

When altering a default ControlTemplate, it is best to start with the whole default XAML and first get that working in your custom ControlTemplate. Once you have it working, then you can start to manipulate it, bit by bit and regularly checking it after every few changes. Then, if it doesn't work one time, you can just undo the last few changes and find out what you did wrong.