Michael Hart
Michael Hart

Reputation: 11

Custom button border style does not change on IsPressed event

I'm trying to recreate a Valve VGUI interface style in WPF. So far I am able to assign a border to a button, but the border style does not change when pressing the button.

Here's my XAML code:

 <Window.Resources>
    <Style x:Key="BorderLight_Style" TargetType="Border">
        <Setter Property="BorderBrush" Value="#889180" />
        <Setter Property="BorderThickness" Value="1,1,0,0" />
    </Style>
    <Style x:Key="BorderShadow_Style" TargetType="Border">
        <Setter Property="BorderBrush" Value="#FF282E22" />
        <Setter Property="BorderThickness" Value="0,0,1,1" />
    </Style>
    <Style x:Key="BorderLight_Style_pressed" TargetType="Border">
        <Setter Property="BorderBrush" Value="#FF282E22" />
        <Setter Property="BorderThickness" Value="1,1,0,0" />
    </Style>
    <Style x:Key="BorderShadow_Style_pressed" TargetType="Border">
        <Setter Property="BorderBrush" Value="#889180" />
        <Setter Property="BorderThickness" Value="0,0,1,1" />
    </Style>
    <Style TargetType="Grid">
        <Setter Property="Background" Value="#FF4C5844" />
    </Style>
    <Style TargetType="Button">
        <Setter Property="Background" Value="#FF4C5844" />
        <Setter Property="Foreground" Value="#D8DED3" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border x:Name="border" Style="{StaticResource BorderShadow_Style}">
                        <Border x:Name="border2" Style="{StaticResource BorderLight_Style}" />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter TargetName="border" Property="Style" Value="{StaticResource BorderShadow_Style_pressed}" />
                            <Setter TargetName="border2" Property="Style" Value="{StaticResource BorderLight_Style_pressed}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

Am I doing anything wrong with this? Thanks!

Upvotes: 1

Views: 431

Answers (1)

Corentin Pane
Corentin Pane

Reputation: 4943

Controls need to have a defined Background property to participate in hit-testing - and thus to allow the IsPressed property to be set on a Button for example.

Set the Background property of your inner Border to any valid value - such as Transparent - to fix your Trigger.


Note: if you were to press TAB to select your button and press the space bar, you would see your Trigger working even though you didn't set it any background, because IsPressed also reacts to the space bar.

Upvotes: 1

Related Questions