WPF 触发器属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15160874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Trigger Properties
提问by Dom
I am fairly new to WPF and I am currently working with triggers
. I have a question regarding a simple trigger. By simple trigger, I mean one that watches for a change in a dependency property
and uses a setter
to change the style.
我对 WPF 还很陌生,目前正在使用triggers
. 我有一个关于简单触发器的问题。通过简单的触发,我的意思是一个用于在一个变化的手表dependency property
,并使用setter
更改样式。
Example:
例子:
<Style.Triggers>
<Trigger Property="Control.IsFocused" Value ="True">
<Setter Property=" Control.Foreground" Value =" DarkRed" />
</Trigger>
</Style.Triggers>
All examples I have seen have used the following trigger properties
:
我见过的所有例子都使用了以下内容trigger properties
:
<Trigger Property="Control.IsFocused" Value ="True">
<Trigger Property="Control.IsMouseOver" Value ="True">
<Trigger Property="Button.IsPressed" Value ="True">
<Trigger Property="Control.IsFocused" Value ="True">
<Trigger Property="Control.IsMouseOver" Value ="True">
<Trigger Property="Button.IsPressed" Value ="True">
Question: Are these the onlytrigger properties available? If not, what others exist?
问题:这些是唯一可用的触发器属性吗?如果没有,还有什么其他存在?
I have searched online but to no avail. Maybe someone could shed some light on this.
我已经在网上搜索过,但无济于事。也许有人可以对此有所了解。
回答by Brian S
These are not the only properties that you can use in your Triggers
, however, they are common examples because they are easily understandable and easy to demonstrate.
这些不是您可以在您的 中使用的唯一属性Triggers
,但是,它们是常见示例,因为它们易于理解且易于演示。
In truth, you can have your Trigger
watch any DependencyProperty
, but because it is "triggered" when the value changes (and matches the Value
you tell it to watch for), it only makes sense to use properties that will change at runtime, often from user action (such as focus, mouse over, pressed, etc). Only certain DependencyProperties
actual change value under these circumstances, so not all of them make sense to use in Triggers
.
事实上,你可以让你的Trigger
watch any DependencyProperty
,但因为它在值改变时“触发”(并匹配Value
你告诉它要注意的),只有使用在运行时会改变的属性才有意义,通常来自用户操作(例如焦点、鼠标悬停、按下等)。DependencyProperties
在这些情况下只有某些实际变化值,因此并非所有这些都有意义在Triggers
.
Microsoft has added several DependencyProperties
to the standard controls so that you can easily create triggers based on changes. However you can also create your own controls with your own DependencyProperties
and have triggers that respond when your custom DependencyProperties
change.
MicrosoftDependencyProperties
在标准控件中添加了几个,以便您可以根据更改轻松创建触发器。但是,您也可以使用自己的控件创建自己的控件,DependencyProperties
并在自定义DependencyProperties
更改时具有响应的触发器。
Keep in mind, PropertyTriggers
are only one flavor of Trigger
in WPF. There are also EventTriggers
and DataTriggers
and MultiTriggers
. These other triggers fire based on events or changes in data, or in the case of MultiTriggers
multiple property (or data) values.
请记住,WPFPropertyTriggers
中只有一种风格Trigger
。还有EventTriggers
andDataTriggers
和MultiTriggers
。这些其他触发器基于事件或数据更改,或者在MultiTriggers
多个属性(或数据)值的情况下触发。
Is there something specific you're trying to do with Triggers
? This other answerprovides a good explanation of what each type of trigger does.
你有什么具体的事情要做Triggers
吗? 这个另一个答案很好地解释了每种类型的触发器的作用。
回答by Rachel
There are multiple types of triggers in WPF, but the two most commonly used are regular Triggers
and DataTriggers
WPF 中有多种类型的触发器,但最常用的两种是常规Triggers
和DataTriggers
Both types of triggers will watch a value, and when it changes to match the specified Value
then they apply your Style Setters.
两种类型的触发器都会监视一个值,当它更改为匹配指定的值时,Value
它们会应用您的样式设置器。
Regular triggerscan be used for any Dependency Property of the object. This includes properties like Text
, Visibility
, Background
, etc in addition to the more commonly triggered properties that you specified: IsFocused
, IsMouseOver
, and IsPressed
.
常规触发器可用于对象的任何依赖属性。这包括像性能Text
,Visibility
,Background
,等除了较为常用的属性触发您指定:IsFocused
,IsMouseOver
,和IsPressed
。
Note that per the MSDN page about Trigger.Property, you don't need to specify the class name prefix if the Style or Template containing the trigger has it's TargetType
property set
请注意,根据有关 Trigger.Property的MSDN 页面,如果包含触发器的样式或模板具有其TargetType
属性集,则不需要指定类名前缀
An easy way to remember it is if you can bind the property, you can set a Trigger on it.
记住它的一种简单方法是,如果您可以绑定属性,则可以在其上设置触发器。
DataTriggersare triggers that watch a bound value instead of a Dependency Property. They allow you to watch a bound expression, and will react when that binding evaluates equal to your Value.
DataTriggers是监视绑定值而不是依赖属性的触发器。它们允许您观察绑定表达式,并在该绑定评估等于您的值时做出反应。
For example, you could set a DataTrigger
on "{Binding Value}"
or "{Binding ElementName=MyTextBox, Path=IsChecked}"
. You can even use Converters
with DataTriggers, such as
例如,您可以设置DataTrigger
on"{Binding Value}"
或"{Binding ElementName=MyTextBox, Path=IsChecked}"
。您甚至可以Converters
与 DataTriggers 一起使用,例如
<DataTrigger
Binding="{Binding SomeInt, Converter={StaticResource IsGreaterThanZero}}"
Value="True">
回答by KaluSinghRao
Use this code for better experience with trigger in wpf.
使用此代码可在 wpf 中获得更好的触发器体验。
<Window x:Class="DataBinding.Trigger2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Trigger2" Height="500" Width="500">
<Window.Resources>
<Style TargetType="Button">
<Style.Setters>
<Setter Property="FontFamily" Value="Tahoma"></Setter>
<Setter Property="FontSize" Value="15"></Setter>
<Setter Property="FontWeight" Value="Bold"></Setter>
<Setter Property="Height" Value="25"></Setter>
<Setter Property="Width" Value="100"></Setter>
</Style.Setters>
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Background" Value="Purple"></Setter>
<Setter Property="Foreground" Value="DarkCyan"></Setter>
<Setter Property="FontFamily" Value="Franklin Gothic"></Setter>
<Setter Property="FontSize" Value="10"></Setter>
<Setter Property="FontWeight" Value="Normal"></Setter>
<Setter Property="Height" Value="50"></Setter>
<Setter Property="Width" Value="200"></Setter>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Red"></Setter>
<Setter Property="Foreground" Value="White"></Setter>
<Setter Property="FontFamily" Value="Calibri"></Setter>
<Setter Property="FontSize" Value="25"></Setter>
<Setter Property="FontWeight" Value="Heavy"></Setter>
<Setter Property="Height" Value="100"></Setter>
<Setter Property="Width" Value="400"></Setter>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="Green"></Setter>
<Setter Property="Foreground" Value="Violet"></Setter>
<Setter Property="FontFamily" Value="Times New Roman"></Setter>
<Setter Property="FontSize" Value="20"></Setter>
<Setter Property="FontWeight" Value="Thin"></Setter>
<Setter Property="Height" Value="250"></Setter>
<Setter Property="Width" Value="250"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Button>It's a Magic.</Button>