触发器和数据处理程序有什么区别?

时间:2022-09-16 09:07:30

They seem the same. Is there a significant difference? I think I am missing something.

他们看起来是一样的。有显著的差异吗?我想我漏掉了什么。

3 个解决方案

#1


37  

A regular trigger only responds to dependency properties.

一个规则触发器只响应依赖属性。

A data trigger can be triggered by any .NET property (by setting its Binding property). However, its setters can still target only dependency properties.

任何. net属性(通过设置其绑定属性)都可以触发数据触发器。但是,它的setter仍然只能针对依赖属性。

#2


20  

Another difference is that a DataTrigger can be bound to another control, a StaticResource, etc etc.

另一个区别是数据处理程序可以绑定到另一个控件、静态源等。

<Style TargetType="TextBox">
  <Style.Triggers>
    <DataTrigger 
      Binding="{Binding SomeProperty, 
                        ElementName=someOtherControl" 
      Value="Derp">
      <!-- etc -->

You can only examine the instance on which the style is set when using a Trigger. For example, a Trigger applied to a Button can inspect the value of IsPressed, but it would not be able to inspect the (for example) Text value of a TextBox on the same form if you wished to disable the Button if the TextBox was empty.

您只能检查在使用触发器时设置样式的实例。例如,应用于按钮的触发器可以检查IsPressed的值,但是如果您希望在文本框为空的情况下禁用按钮,它将不能检查同一窗体上的文本框的文本值(例如)。

#3


15  

The short answer (as I'm about to sleep)- A trigger works on dependency properties (typically GUI properties) whereas data triggers can be triggered by any .NET property (typically a property in a ViewModel that implements INotifyPropertyChanged).

简短的回答(当我要睡觉时)——触发器作用于依赖属性(通常是GUI属性),而数据触发器可以被任何。net属性触发(通常是实现INotifyPropertyChanged的ViewModel中的属性)。

#1


37  

A regular trigger only responds to dependency properties.

一个规则触发器只响应依赖属性。

A data trigger can be triggered by any .NET property (by setting its Binding property). However, its setters can still target only dependency properties.

任何. net属性(通过设置其绑定属性)都可以触发数据触发器。但是,它的setter仍然只能针对依赖属性。

#2


20  

Another difference is that a DataTrigger can be bound to another control, a StaticResource, etc etc.

另一个区别是数据处理程序可以绑定到另一个控件、静态源等。

<Style TargetType="TextBox">
  <Style.Triggers>
    <DataTrigger 
      Binding="{Binding SomeProperty, 
                        ElementName=someOtherControl" 
      Value="Derp">
      <!-- etc -->

You can only examine the instance on which the style is set when using a Trigger. For example, a Trigger applied to a Button can inspect the value of IsPressed, but it would not be able to inspect the (for example) Text value of a TextBox on the same form if you wished to disable the Button if the TextBox was empty.

您只能检查在使用触发器时设置样式的实例。例如,应用于按钮的触发器可以检查IsPressed的值,但是如果您希望在文本框为空的情况下禁用按钮,它将不能检查同一窗体上的文本框的文本值(例如)。

#3


15  

The short answer (as I'm about to sleep)- A trigger works on dependency properties (typically GUI properties) whereas data triggers can be triggered by any .NET property (typically a property in a ViewModel that implements INotifyPropertyChanged).

简短的回答(当我要睡觉时)——触发器作用于依赖属性(通常是GUI属性),而数据触发器可以被任何。net属性触发(通常是实现INotifyPropertyChanged的ViewModel中的属性)。