WPF样式触发器:我可以将一种样式应用于多种属性吗?

编程入门 行业动态 更新时间:2024-10-21 15:49:41
本文介绍了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样式触发器:我可以将一种样式应用于多种属性吗?

本文发布于:2023-10-28 01:10:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535058.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:样式   触发器   应用于   属性   多种

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!