WPF样式触发器:我可以将一种样式应用于多种属性吗? [英] WPF Style Triggers: can I apply the one style for a variety of Properties?

查看:67
本文介绍了WPF样式触发器:我可以将一种样式应用于多种属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎必须要有一种方法:

It seems like there has to be a way to do this:

我正在基于两个属性触发器在列表框中应用ItemContainerStyle.如您所见,我使用的是完全相同的一组触发器输入/退出操作,仅应用于两个不同的属性.是否有与< Trigger Property ="prop1"或Property ="prop2">等效的东西? (显然不会像那样,但这很可能会弄清楚这一点.)

I am applying an ItemContainerStyle in my Listbox, based on two property triggers. As you can see, I'm using the exact same set of trigger enter/exit actions, simply applied on two different properties. Is there something equivalent to a <Trigger Property="prop1" OR Property="prop2"> ??? (Obviously wouldn't look like that, but that probably gets the point across.)

<Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
            <Style.Triggers>
                <Trigger Property="IsKeyboardFocusWithin" Value="True">
                    <Trigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="Height"
                                        To="50" Duration="0:0:.3"></DoubleAnimation>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                    <Trigger.ExitActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="Height"
                                              To="25" Duration="0:0:.3" />
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.ExitActions>
                </Trigger>


            </Style.Triggers>


   </Style>
<Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Trigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="Height"
                                        To="50" Duration="0:0:.3"></DoubleAnimation>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                    <Trigger.ExitActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="Height"
                                              To="25" Duration="0:0:.3" />
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.ExitActions>
                </Trigger>


            </Style.Triggers>
</Style>

推荐答案

您是否尝试过以下任何一项(摘自Adam Nathan的书: Windows Presentation Foundation Unleashed ):

Have you tried any of the following (extracted from Adam Nathan's book: Windows Presentation Foundation Unleashed):

  • 已将多个触发器应用于同一元素(以获取逻辑OR).
  • 为同一触发器评估了多个属性(以获得逻辑AND).

由于Style.Triggers可以包含多个触发器,因此可以使用完全相同的Setter创建多个触发器来表达逻辑OR关系.

Since Style.Triggers can contain multiple triggers, you can create more than one with the exact same Setters to express a logical OR relationship.

例如:

<Style.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
        ...
    </Trigger>
    <Trigger Property="IsKeyboardFocusWithin" Value="True">
        ....
    </Trigger>
</Style.Triggers>

这意味着,如果 IsMouseOver ,或者如果 IsKeyboardFocusWithin 是true,则应用操作.

This means, "if IsMouseOver is true or if IsKeyboardFocusWithin is true, apply the action.

要表达逻辑AND关系,可以使用称为 MultiTrigger Trigger 变体,或称为 DataTrigger 的变体MultiDataTrigger .这两个触发器都有一个条件集合,其中包含您通常直接放入触发器或DataTrigger中的信息.

To express a logical AND relationship, you can use a variation of Trigger called MultiTrigger, or a variation of DataTrigger called MultiDataTrigger. Both triggers have a collection of Conditions that contain the information you would normally put directly inside a Trigger or DataTrigger.

例如:

<Style.Triggers>
    <MultiTrigger>
    <MultiTrigger.Conditions>
        <Condition Property="IsMouseOver" Value="True"/>
        <Condition Property="IsKeyboardFocusWithin" Value="True"/>
    </MultiTrigger.Conditions>
    </MultiTrigger>
        <Setter ...>
        <Setter ...>
</Style.Triggers>

这篇关于WPF样式触发器:我可以将一种样式应用于多种属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆