Problem with WPF style trigger

I want to write a style for a Border element when I click it. But does not have the IsPressed property. So how can I set the style for this scenario. Please, help.

+3
source share
3 answers

Instead, use Buttonand redefine its template so that it looks like 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>
+3
source

Blissfully copied from Thomas's 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>
+1
source

use the MouseRightButtonDown event with event triggers

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

and etc.

0
source

Source: https://habr.com/ru/post/1763222/


All Articles