DataTrigger Class
Collapse the table of content
Expand the table of content

DataTrigger Class


Represents a trigger that applies property values or performs actions when the bound data meets a specified condition.

Namespace:   System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)

type DataTrigger = 
        inherit TriggerBase
        interface IAddChild


Initializes a new instance of the DataTrigger class.


Gets or sets the binding that produces the property value of the data object.


Gets the DependencyObjectType that wraps the CLR type of this instance. (Inherited from DependencyObject.)


Gets the Dispatcher this DispatcherObject is associated with. (Inherited from DispatcherObject.)


Gets a collection of TriggerAction objects to apply when the trigger object becomes active. This property does not apply to the EventTrigger class.(Inherited from TriggerBase.)


Gets a collection of TriggerAction objects to apply when the trigger object becomes inactive. This property does not apply to the EventTrigger class.(Inherited from TriggerBase.)


Gets a value that indicates whether this instance is currently sealed (read-only).(Inherited from DependencyObject.)


Gets a collection of Setter objects, which describe the property values to apply when the data item meets the specified condition.


Gets or sets the value to be compared with the property value of the data object.


Determines whether the calling thread has access to this DispatcherObject.(Inherited from DispatcherObject.)


Clears the local value of a property. The property to be cleared is specified by a DependencyProperty identifier. (Inherited from DependencyObject.)


Clears the local value of a read-only property. The property to be cleared is specified by a DependencyPropertyKey. (Inherited from DependencyObject.)


Coerces the value of the specified dependency property. This is accomplished by invoking any CoerceValueCallback function specified in property metadata for the dependency property as it exists on the calling DependencyObject.(Inherited from DependencyObject.)


Determines whether a provided DependencyObject is equivalent to the current DependencyObject.(Inherited from DependencyObject.)


Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)


Gets a hash code for this DependencyObject.(Inherited from DependencyObject.)


Creates a specialized enumerator for determining which dependency properties have locally set values on this DependencyObject. (Inherited from DependencyObject.)


Gets the Type of the current instance.(Inherited from Object.)


Returns the current effective value of a dependency property on this instance of a DependencyObject. (Inherited from DependencyObject.)


Re-evaluates the effective value for the specified dependency property(Inherited from DependencyObject.)


Creates a shallow copy of the current Object.(Inherited from Object.)


Invoked whenever the effective value of any dependency property on this DependencyObject has been updated. The specific dependency property that changed is reported in the event data. (Inherited from DependencyObject.)


Returns the local value of a dependency property, if it exists. (Inherited from DependencyObject.)

System_CAPS_pubmethodSystem_CAPS_staticReceiveMarkupExtension(Object, XamlSetMarkupExtensionEventArgs)

Handles cases where a markup extension provides a value for a property of a DataTrigger object.

System_CAPS_pubmethodSetCurrentValue(DependencyProperty, Object)

Sets the value of a dependency property without changing its value source. (Inherited from DependencyObject.)

System_CAPS_pubmethodSetValue(DependencyProperty, Object)

Sets the local value of a dependency property, specified by its dependency property identifier. (Inherited from DependencyObject.)

System_CAPS_pubmethodSetValue(DependencyPropertyKey, Object)

Sets the local value of a read-only dependency property, specified by the DependencyPropertyKey identifier of the dependency property. (Inherited from DependencyObject.)


Returns a value that indicates whether serialization processes should serialize the value for the provided dependency property.(Inherited from DependencyObject.)


Returns a string that represents the current object.(Inherited from Object.)


Enforces that the calling thread has access to this DispatcherObject.(Inherited from DispatcherObject.)


This API supports the product infrastructure and is not intended to be used directly from your code. Adds a child object.


This API supports the product infrastructure and is not intended to be used directly from your code. Adds the text content of a node to the object.

Style, ControlTemplate, and DataTemplate all have a triggers collection. A DataTrigger allows you to set property values when the property value of the data object matches a specified Value. For example, if you are displaying a list of Employee objects, you may want the foreground color to be different based on each Employee's current attendance. (For example, Employees who are currently on vacation are displayed with a purple foreground.) In some scenarios it may be more suitable to create a converter or to use a DataTemplateSelector. For more information, see the Data Templating Overview.

Note that you must specify both the Binding and Value properties on a DataTrigger for the data trigger to be meaningful. If one or both of the properties are not specified, an exception is thrown.

The Setters property of a DataTrigger object can only consist of Setter objects. Adding a Setter child to a DataTrigger object implicitly adds it to the SetterBaseCollection for the DataTrigger object. EventSetter objects are not supported; only Style.Setters supports EventSetter objects.

In the following example, the ItemsSource of the ListBox is bound to Places, an ObservableCollection<'T> of Place objects. Place objects have properties Name and State.

Each ListBoxItem of the ListBox displays a Place object. The Style in the example is applied to each ListBoxItem.

The DataTrigger is specified such that if the State of the Place data item is "WA" then the foreground of the corresponding ListBoxItem is set to Red.

  <c:Places x:Key="PlacesData"/>

  <Style TargetType="ListBoxItem">
      <DataTrigger Binding="{Binding Path=State}" Value="WA">
        <Setter Property="Foreground" Value="Red" />
          <Condition Binding="{Binding Path=Name}" Value="Portland" />
          <Condition Binding="{Binding Path=State}" Value="OR" />
        <Setter Property="Background" Value="Cyan" />

  <DataTemplate DataType="{x:Type c:Place}">
    <Canvas Width="160" Height="20">
      <TextBlock FontSize="12"
  		   Width="130" Canvas.Left="0" Text="{Binding Path=Name}"/>
      <TextBlock FontSize="12" Width="30"
                 Canvas.Left="130" Text="{Binding Path=State}"/>

  <TextBlock FontSize="18" Margin="5" FontWeight="Bold"
    HorizontalAlignment="Center">Data Trigger Sample</TextBlock>
  <ListBox Width="180" HorizontalAlignment="Center" Background="Honeydew"
    ItemsSource="{Binding Source={StaticResource PlacesData}}"/>

The following example shows how to use a DataTrigger, a BeginStoryboard action, and a Storyboard to animate a property when bound data meets a specified condition. The example displays inventory information in a ListBox control. It uses a DataTrigger to animate the Opacity of every ListBoxItem that contains an out-of-stock book.


    <XmlDataProvider x:Key="InventoryData" XPath="Inventory/Books">
        <Inventory xmlns="">
            <Book ISBN="0-7356-0562-9" Stock="in" Number="9">
              <Title>XML in Action</Title>
              <Summary>XML Web Technology</Summary>
            <Book ISBN="0-7356-1370-2" Stock="in" Number="8">
              <Title>Programming Microsoft Windows With C#</Title>
              <Summary>C# Programming using the .NET Framework</Summary>
            <Book ISBN="0-7356-1288-9" Stock="out" Number="7">
              <Title>Inside C#</Title>
              <Summary>C# Language Programming</Summary>
            <Book ISBN="0-7356-1377-X" Stock="in" Number="5">
              <Title>Introducing Microsoft .NET</Title>
              <Summary>Overview of .NET Technology</Summary>
            <Book ISBN="0-7356-1448-2" Stock="out" Number="4">
              <Title>Microsoft C# Language Specifications</Title>
              <Summary>The C# language definition</Summary>
            <CD Stock="in" Number="3">
              <Title>Classical Collection</Title>
              <Summary>Classical Music</Summary>
            <CD Stock="out" Number="9">
              <Title>Jazz Collection</Title>
              <Summary>Jazz Music</Summary>

    <Style x:Key="AnimatedListBoxItemStyle" TargetType="{x:Type ListBoxItem}">

      <Setter Property="Margin" Value="0,2,0,2" />
      <Setter Property="Padding" Value="0,2,0,2" />

         Binding="{Binding XPath=@Stock}" 
                 From="0.25" To="0.5" Duration="0:0:1" 
              <Storyboard FillBehavior="Stop">
                  To="1" Duration="0:0:1" />


    <ListBox HorizontalAlignment="Center"
      ItemContainerStyle="{StaticResource AnimatedListBoxItemStyle}"
        <Binding Source="{StaticResource InventoryData}"

          <TextBlock FontSize="12" Margin="0,0,10,0">
              <Binding XPath="Title"/>

.NET Framework
Available since 3.0

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
© 2015 Microsoft