Modifier

Partager via


DataTrigger Class

Definition

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

public ref class DataTrigger : System::Windows::TriggerBase, System::Windows::Markup::IAddChild
[System.Windows.Markup.ContentProperty("Setters")]
public class DataTrigger : System.Windows.TriggerBase, System.Windows.Markup.IAddChild
[System.Windows.Markup.ContentProperty("Setters")]
[System.Windows.Markup.XamlSetMarkupExtension("ReceiveMarkupExtension")]
public class DataTrigger : System.Windows.TriggerBase, System.Windows.Markup.IAddChild
[<System.Windows.Markup.ContentProperty("Setters")>]
type DataTrigger = class
    inherit TriggerBase
    interface IAddChild
[<System.Windows.Markup.ContentProperty("Setters")>]
[<System.Windows.Markup.XamlSetMarkupExtension("ReceiveMarkupExtension")>]
type DataTrigger = class
    inherit TriggerBase
    interface IAddChild
Public Class DataTrigger
Inherits TriggerBase
Implements IAddChild
Inheritance
Attributes
Implements

Examples

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.

<Window.Resources>
  <c:Places x:Key="PlacesData"/>

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

  <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}"/>
    </Canvas>
  </DataTemplate>
</Window.Resources>

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

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.

<Page 
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="DataTriggerStoryboardExample">

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

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

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

      <Style.Triggers>
        <DataTrigger 
         Binding="{Binding XPath=@Stock}" 
         Value="out">
          <DataTrigger.EnterActions>
            <BeginStoryboard>
              <Storyboard>
                <DoubleAnimation
                 Storyboard.TargetProperty="Opacity"
                 From="0.25" To="0.5" Duration="0:0:1" 
                 RepeatBehavior="Forever"
                 AutoReverse="True"/>
              </Storyboard>
            </BeginStoryboard>
          </DataTrigger.EnterActions>
          <DataTrigger.ExitActions>
            <BeginStoryboard> 
              <Storyboard FillBehavior="Stop">
                <DoubleAnimation
                  Storyboard.TargetProperty="Opacity"
                  To="1" Duration="0:0:1" />
              </Storyboard>
            </BeginStoryboard>  
          </DataTrigger.ExitActions>
          
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </Page.Resources>

  <StackPanel>
    <ListBox HorizontalAlignment="Center"
      ItemContainerStyle="{StaticResource AnimatedListBoxItemStyle}"
      Padding="2">
      <ListBox.ItemsSource>
        <Binding Source="{StaticResource InventoryData}"
                 XPath="*"/>
      </ListBox.ItemsSource>

      <ListBox.ItemTemplate>
        <DataTemplate>
          <TextBlock FontSize="12" Margin="0,0,10,0">
            <TextBlock.Text>
              <Binding XPath="Title"/>
            </TextBlock.Text>
          </TextBlock>
        </DataTemplate>
      </ListBox.ItemTemplate>
    </ListBox>
  </StackPanel>
</Page>

Remarks

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.

Constructors

DataTrigger()

Initializes a new instance of the DataTrigger class.

Properties

Binding

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

DependencyObjectType

Gets the DependencyObjectType that wraps the CLR type of this instance.

(Inherited from DependencyObject)
Dispatcher

Gets the Dispatcher this DispatcherObject is associated with.

(Inherited from DispatcherObject)
EnterActions

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

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

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

(Inherited from DependencyObject)
Setters

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

Value

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

Methods

CheckAccess()

Determines whether the calling thread has access to this DispatcherObject.

(Inherited from DispatcherObject)
ClearValue(DependencyProperty)

Clears the local value of a property. The property to be cleared is specified by a DependencyProperty identifier.

(Inherited from DependencyObject)
ClearValue(DependencyPropertyKey)

Clears the local value of a read-only property. The property to be cleared is specified by a DependencyPropertyKey.

(Inherited from DependencyObject)
CoerceValue(DependencyProperty)

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)
Equals(Object)

Determines whether a provided DependencyObject is equivalent to the current DependencyObject.

(Inherited from DependencyObject)
GetHashCode()

Gets a hash code for this DependencyObject.

(Inherited from DependencyObject)
GetLocalValueEnumerator()

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

(Inherited from DependencyObject)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
GetValue(DependencyProperty)

Returns the current effective value of a dependency property on this instance of a DependencyObject.

(Inherited from DependencyObject)
InvalidateProperty(DependencyProperty)

Re-evaluates the effective value for the specified dependency property.

(Inherited from DependencyObject)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
OnPropertyChanged(DependencyPropertyChangedEventArgs)

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)
ReadLocalValue(DependencyProperty)

Returns the local value of a dependency property, if it exists.

(Inherited from DependencyObject)
ReceiveMarkupExtension(Object, XamlSetMarkupExtensionEventArgs)

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

SetCurrentValue(DependencyProperty, Object)

Sets the value of a dependency property without changing its value source.

(Inherited from DependencyObject)
SetValue(DependencyProperty, Object)

Sets the local value of a dependency property, specified by its dependency property identifier.

(Inherited from DependencyObject)
SetValue(DependencyPropertyKey, Object)

Sets the local value of a read-only dependency property, specified by the DependencyPropertyKey identifier of the dependency property.

(Inherited from DependencyObject)
ShouldSerializeProperty(DependencyProperty)

Returns a value that indicates whether serialization processes should serialize the value for the provided dependency property.

(Inherited from DependencyObject)
ToString()

Returns a string that represents the current object.

(Inherited from Object)
VerifyAccess()

Enforces that the calling thread has access to this DispatcherObject.

(Inherited from DispatcherObject)

Explicit Interface Implementations

IAddChild.AddChild(Object)

Adds a child object.

IAddChild.AddText(String)

Adds the text content of a node to the object.

Applies to

See also