WPF样式触发器问题

时间:2010-09-04 13:05:45

标签: wpf

我想在按下时为Border元素编写一个Style。但是没有IsPressed属性。那么我该如何为这个场景设置样式呢。请帮忙。

3 个答案:

答案 0 :(得分:3)

改为使用Button,然后重新定义其模板,使其显示为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>

答案 1 :(得分:1)

从托马斯的例子中大肆复制并扩展它:

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

答案 2 :(得分:0)

将MouseRightButtonDown事件与event triggers

一起使用
<Style TargetType="Border">
....
  <Style.Triggers>
    <EventTrigger RoutedEvent="Border.MouseRightButtonDown">