StyleSelector StyleSelector StyleSelector StyleSelector Class


Provides a way to apply styles based on custom logic.

public ref class StyleSelector
public class StyleSelector
type StyleSelector = class
Public Class StyleSelector


The following example shows how to define a StyleSelector that defines a Style for a row. This example defines the Background color according to the row index.

public class ListViewItemStyleSelector : StyleSelector
    public override Style SelectStyle(object item, 
        DependencyObject container)
        Style st = new Style();
        st.TargetType = typeof(ListViewItem);
        Setter backGroundSetter = new Setter();
        backGroundSetter.Property = ListViewItem.BackgroundProperty;
        ListView listView = 
              as ListView;
        int index = 
        if (index % 2 == 0)
            backGroundSetter.Value = Brushes.LightBlue;
            backGroundSetter.Value = Brushes.Beige;
        return st;
Public Class ListViewItemStyleSelector
    Inherits StyleSelector
    Public Overrides Function SelectStyle(ByVal item As Object, ByVal container As DependencyObject) As Style
        Dim st As New Style()
        st.TargetType = GetType(ListViewItem)
        Dim backGroundSetter As New Setter()
        backGroundSetter.Property = ListViewItem.BackgroundProperty
        Dim listView As ListView = TryCast(ItemsControl.ItemsControlFromItemContainer(container), ListView)
        Dim index As Integer = listView.ItemContainerGenerator.IndexFromContainer(container)
        If index Mod 2 = 0 Then
            backGroundSetter.Value = Brushes.LightBlue
            backGroundSetter.Value = Brushes.Beige
        End If
        Return st
    End Function
End Class

The following example shows how to define a ResourceKey for the StyleSelector. The namespc prefix maps to a CLR namespace and the corresponding assembly where the StyleSelector is defined. For more information, see XAML Namespaces and Namespace Mapping for WPF XAML.

<namespc:ListViewItemStyleSelector x:Key="myStyleSelector"/>

The following example shows how to set the ItemContainerStyleSelector property of a ListView to this StyleSelector resource.

      ItemsSource="{Binding Source={StaticResource EmployeeData}, 
      ItemContainerStyleSelector="{DynamicResource myStyleSelector}" >      
      <GridViewColumn DisplayMemberBinding="{Binding XPath=FirstName}" 
                      Header="First Name" Width="120"/>
      <GridViewColumn DisplayMemberBinding="{Binding XPath=LastName}" 
                      Header="Last Name" Width="120"/>
      <GridViewColumn DisplayMemberBinding="{Binding XPath=FavoriteCity}" 
                      Header="Favorite City" Width="120"/>

For an example of how to create a selector to choose a defined style resource, see the implementation of DataTemplateSelector.SelectTemplate, which allows you to use custom logic to select a DataTemplate, based on a similar concept.


To create a StyleSelector that applies a style based on custom logic, create a subclass of the StyleSelector class and implement the SelectStyle method.


StyleSelector() StyleSelector() StyleSelector() StyleSelector()

Initializes a new instance of a StyleSelector.


Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
SelectStyle(Object, DependencyObject) SelectStyle(Object, DependencyObject) SelectStyle(Object, DependencyObject) SelectStyle(Object, DependencyObject)

When overridden in a derived class, returns a Style based on custom logic.

ToString() ToString() ToString() ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

See also