3

我在侧面板中有一组按钮。我想更改已单击按钮的背景。我已经尝试过使用style.trigger并且我能想到的唯一属性是IsPressed,但这并没有太大帮助,因为它会改变背景一秒钟(直到按下按钮[duh])。

这是我尝试过的代码:

<Style.Triggers>
<Trigger Property="IsPressed" Value="True">
    <Setter Property="Background" Value="SlateGray" />
    <Setter Property="Foreground" Value="White"></Setter>
</Trigger>
</Style.Triggers>

我能想到的另一种方法是使用 a 为每个按钮创建单独的样式,datatrigger因为我的属性会随着按钮的选择而变化,但这似乎有点矫枉过正。知道如何突出显示已单击的按钮吗?

4

2 回答 2

11

这种触发器在你的条件满足时运行,然后效果消失。为了设置好而不是一段时间,看看这个

<Button Content="Content" Background="Red">
        <Button.Triggers>
            <EventTrigger RoutedEvent="MouseEnter">
                <BeginStoryboard>
                    <Storyboard>
                        <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" To="CadetBlue"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Button.Triggers>
    </Button>

由于 IsPressed 不是 RoutedEvent 您可以使用它

 <Button Content="Content" Background="Red">
        <Button.Style>
            <Style TargetType="Button">
                <Style.Triggers>
                    <Trigger Property="IsPressed" Value="True">
                        <Trigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" To="CadetBlue"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.EnterActions>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>
于 2014-08-20T21:43:16.350 回答
0
<UserControl.Resources>
    <Style TargetType="{x:Type Button}">
        <Setter Property="Background" Value="Green"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border Background="{TemplateBinding Background}" BorderBrush="Black" BorderThickness="1">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsFocused" Value="True">
                <Setter Property="Background" Value="Red"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>
<Grid RenderTransformOrigin="0.578,0.503">
    <Button Width="100" Height="50" Margin="265,265,435,135"/>
    <Button Height="50" Margin="400,202,302,198"/>
</Grid>
于 2021-09-07T06:01:02.667 回答