Partager via


How to: Use Triggers to Style Selected Items in a ListView

This example shows how to define Triggers for a ListViewItem control so that when a property value of a ListViewItem changes, the Style of the ListViewItem changes in response.

Example

If you want the Style of a ListViewItem to change in response to property changes, define Triggers for the Style change.

The following example defines a Trigger that sets the Foreground property to Blue and changes the Cursor to display a Hand when the IsMouseOver property changes to true.

<Style x:Key="MyContainer" TargetType="{x:Type ListViewItem}">

  <Setter Property="Margin" Value="0,1,0,0"/>
  <Setter Property="Height" Value="21"/>

  <Style.Triggers>

...

    <Trigger Property="IsMouseOver" Value="true">
      <Setter Property="Foreground" Value="Blue" />
      <Setter Property="Cursor" Value="Hand"/>
    </Trigger>

...

  </Style.Triggers>
</Style>

The following example defines a MultiTrigger that sets the Foreground property of a ListViewItem to Yellow when the ListViewItem is the selected item and has keyboard focus.

<Style x:Key="MyContainer" TargetType="{x:Type ListViewItem}">

  <Setter Property="Margin" Value="0,1,0,0"/>
  <Setter Property="Height" Value="21"/>

  <Style.Triggers>

...

    <MultiTrigger>
      <MultiTrigger.Conditions>
        <Condition Property="IsSelected" Value="true" />
        <Condition Property="Selector.IsSelectionActive" Value="true" />
      </MultiTrigger.Conditions>
      <Setter Property="Foreground" Value="Yellow" />
    </MultiTrigger>

...

  </Style.Triggers>
</Style>

For the complete sample, see ListView with Styled ListViewItems Sample.

See Also

Reference

Control
ListView
GridView

Concepts

ListView Overview
GridView Overview

Other Resources

ListView How-to Topics
ListView Samples