Binding Property
Collapse the table of content
Expand the table of content

Condition.Binding Property

 

Gets or sets the binding that specifies the property of the condition. This is only applicable to MultiDataTrigger objects.

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

Public Property Binding As BindingBase

Property Value

Type: System.Windows.Data.BindingBase

The default value is null.

MultiDataTriggers allow you to set property values based on values of the data that is returned. For example, if you are displaying a list of task items, you may want to display a task with a red background if it is of high priority and has not been looked at for more than two weeks.

You create a binding and use the Path property to bind to a property of a certain object (the binding source object). For example, you may bind to the Priority property of a Task. For more information, see Data Binding Overview.

Note that if it is a condition for a MultiDataTrigger, the Binding and Value properties must be set. Setting the Property value would cause an exception in that case.

<object property="{Binding declaration}"/>

<object>
  <object.Binding>
    <Binding …/>
  </object.Binding>
</object>

declaration

A binding declaration. See Binding Declarations Overview for more information.

In the following example, the ItemsSource of the ListBox is bound to Places, an ObservableCollection(Of 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 Conditions of the MultiDataTrigger are specified such that if the Name and State of the Place data item is "Portland" and "OR" respectively, then the background of corresponding ListBoxItem is set to Cyan.

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

.NET Framework
Available since 3.0
Return to top
Show:
© 2016 Microsoft