What's the difference between a Trigger and a DataTrigger?

巧了我就是萌 提交于 2019-12-03 08:58:24

问题


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


回答1:


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.




回答2:


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.




回答3:


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).



来源:https://stackoverflow.com/questions/6499124/whats-the-difference-between-a-trigger-and-a-datatrigger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!