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

WpfTriggersDatatrigger

Wpf Problem Overview


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

Wpf Solutions


Solution 1 - Wpf

A regular Trigger only responds to dependency properties.

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

Solution 2 - Wpf

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.

Solution 3 - Wpf

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJerry NixonView Question on Stackoverflow
Solution 1 - WpfSean UView Answer on Stackoverflow
Solution 2 - Wpfuser1228View Answer on Stackoverflow
Solution 3 - WpfRichardODView Answer on Stackoverflow