WPF样式触发器问题

3

我想为一个边框元素写一个样式,当它被按下时。但是它没有IsPressed属性。那么我该如何设置这种情况的样式呢?请帮忙。

3个回答

3
请使用一个按钮代替,并重新定义其模板,以使其呈现为边框:
<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
虽然总体思路是正确的(而且聪明),但此代码中有两个错误:它应为<Button.Template>,并且 <Border> 需要一个嵌套的<ContentPresenter>标记。 - Dabblernl

1
< p > 明目张胆地从 Thomas 的例子上抄袭并对其进行扩展:

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

0

使用MouseRightButtonDown事件与事件触发器

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

等等。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接