tags:

views:

49

answers:

3

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.

A: 

use MouseRightButtonDown event with event triggers

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

etc.

lukas
Since you can't add a setter to an EventTrigger, this isn't gonna help...
Thomas Levesque
+2  A: 

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>
Thomas Levesque
While the general idea is correct (and clever) there are two mistakes in this code: It is `<Button.Template`> and '<Border`> needs a nested `<ContentPresenter`> tag.
Dabblernl
+1  A: 

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>
Dabblernl