单击期间WPF按钮更改背景

时间:2016-04-21 15:39:06

标签: wpf

这应该很简单,因此这是我所缺少的。我需要在仅限点击期间更改按钮的背景颜色。因此,如果用户单击按钮2天,则在该两天期间颜色会发生变化。用户完成后单击它将返回正常颜色。想到这将解决我的问题:

<Style x:Key="MyButtonStyle" TargetType="Button">
<Setter Property="Background" Value="Red"/>
<Setter Property="Button.Effect">
    <Setter.Value>
        <DropShadowEffect Color="Black" Direction="320" ShadowDepth="3" BlurRadius="5" Opacity="0.5" />
    </Setter.Value>
</Setter>
<Style.Triggers>
    <Trigger Property="IsPressed" Value="True">
        <Setter Property="Button.Background" Value="Purple" />
        <Setter Property="Button.Effect">
            <Setter.Value>
                <DropShadowEffect Color="Black" Direction="320" ShadowDepth="0" BlurRadius="0" Opacity="0" />
            </Setter.Value>
        </Setter>
    </Trigger>
</Style.Triggers>

现在我知道IsPressed确实有效,因为DropShadowEffect在新闻期间正常工作。但是颜色不会改变。我能想到为什么IsMouseOver优先于IsPressed的唯一原因。如果是这种情况,我怎样才能让这两个事件一起工作?&#34;

现在人们开始将我链接到此之前;

WPF changing button background on click或此Change Button Background color on EventTrigger in WPF或此Change Button Background color on EventTrigger in WPF

是的,这些会更改按钮的颜色,但无论用户点击时间如何。如上所述,我只想在实际用户点击期间实现,例如DropShadowEffect

1 个答案:

答案 0 :(得分:1)

啊,我是对的!问题实际上是IsMouseOverIsPressed不相处的地方!这是修复;

<Style x:Key="NewButton" TargetType="Button">
<Setter Property="Background" Value="Red"/>
<Setter Property="Button.Effect">
    <Setter.Value>
        <DropShadowEffect Color="Black" Direction="320" ShadowDepth="3" BlurRadius="5" Opacity="0.5" />
    </Setter.Value>
</Setter>
<Style.Triggers>
    <MultiTrigger>
        <MultiTrigger.Conditions>
            <Condition Property="IsPressed" Value="False" />
            <Condition Property="IsMouseOver" Value="True" />
        </MultiTrigger.Conditions>
        <Setter Property="Background" Value="Green" />
    </MultiTrigger>
    <Trigger Property="IsPressed" Value="True">
        <Setter Property="Button.Background" Value="Purple" />
        <Setter Property="Button.Effect">
            <Setter.Value>
                <DropShadowEffect Color="Black" Direction="320" ShadowDepth="0" BlurRadius="0" Opacity="0" />
            </Setter.Value>
        </Setter>
    </Trigger>
</Style.Triggers>