munna
munna

Reputation: 229

WPF Style trigger problem

I want to write a Style for a Border element when its pressed. But doesn't have IsPressed property. So how can I set style for this scenario. Please help.

Upvotes: 3

Views: 2274

Answers (3)

Dabblernl
Dabblernl

Reputation: 16121

Blatantly copied from Thomas' example and expanded on it:

<Window x:Class="BorderpressSpike.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style x:Key="NormalBorderStyle" TargetType="Border">
            <Setter Property="BorderThickness" Value="2"/>
            <Setter Property="BorderBrush" Value="Black"/>
        </Style>
        <Style x:Key="PressedBorderStyle" TargetType="Border">
            <Setter Property="BorderThickness" Value="5"/>
            <Setter Property="BorderBrush" Value="Red"/>
        </Style>

    </Window.Resources>
    <StackPanel>
        <Button Height="500">
            <Button.Template>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border Name="bd" Style="{StaticResource NormalBorderStyle}" >
                        <ContentPresenter/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter TargetName="bd" 
                            Property="Style" 
                            Value="{StaticResource PressedBorderStyle}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Button.Template>
            <Button Height="300">Inside Pressable Border</Button>
        </Button>

    </StackPanel>
</Window>

Upvotes: 1

Thomas Levesque
Thomas Levesque

Reputation: 292465

Use a Button instead, and redefine its template so that it appears as a Border:

<Button>
    <Button.ControlTemplate>
        <ControlTemplate TargetType="{x:Type Button}">
            <Border Name="bd" Style="{StaticResource NormalBorderStyle}">
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsPressed" Value="True">
                    <Setter TargetName="bd"
                            Property="Style"
                            Value="{StaticResource PressedBorderStyle}" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Button.ControlTemplate>
</Button>

Upvotes: 3

Lukasz Madon
Lukasz Madon

Reputation: 14994

use MouseRightButtonDown event with event triggers

<Style TargetType="Border">
....
  <Style.Triggers>
    <EventTrigger RoutedEvent="Border.MouseRightButtonDown">

etc.

Upvotes: 0

Related Questions