Esta documentación está archivada y no tiene mantenimiento.

DataTrigger (Clase)

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

Espacio de nombres: System.Windows
Ensamblado: PresentationFramework (en presentationframework.dll)
Espacio de nombres XML:

public class DataTrigger : TriggerBase, IAddChild
/** @attribute ContentPropertyAttribute("Setters") */ 
public class DataTrigger extends TriggerBase implements IAddChild
public class DataTrigger extends TriggerBase implements IAddChild

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 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 of Place objects. Place objects have properties Name and State. The definition of Place and Places are not shown but can be found in the Binding Using Data Triggers Sample.

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}}"/>

Más código

How to: Trigger an Animation When Data Changes

This example shows how to use a DataTrigger, a BeginStoryboard action, and a Storyboard to animate a property when bound data meets a specified condition. A DataTrigger can be used in a Style, ControlTemplate, or DataTemplate.

Los miembros estáticos públicos (Shared en Visual Basic) de este tipo son seguros para la ejecución de subprocesos. No se garantiza que los miembros de instancias sean seguros para la ejecución de subprocesos.

Microsoft .NET Framework 3.0 es compatible con Windows Vista, Microsoft Windows XP SP2 y Windows Server 2003 SP1.

.NET Framework

Compatible con: 3.0