StyleSelector Class
Provides a way to apply styles based on custom logic.
Assembly: PresentationFramework (in PresentationFramework.dll)
| Name | Description | |
|---|---|---|
![]() | StyleSelector() | Initializes a new instance of a StyleSelector. |
| Name | Description | |
|---|---|---|
![]() | Equals(Object) | Determines whether the specified object is equal to the current object.(Inherited from Object.) |
![]() | Finalize() | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.) |
![]() | GetHashCode() | Serves as the default hash function. (Inherited from Object.) |
![]() | GetType() | |
![]() | MemberwiseClone() | |
![]() | SelectStyle(Object, DependencyObject) | When overridden in a derived class, returns a Style based on custom logic. |
![]() | ToString() | Returns a string that represents the current object.(Inherited from Object.) |
To create a StyleSelector that applies a style based on custom logic, create a subclass of the StyleSelector class and implement the SelectStyle method.
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 = ItemsControl.ItemsControlFromItemContainer(container) as ListView; int index = listView.ItemContainerGenerator.IndexFromContainer(container); if (index % 2 == 0) { backGroundSetter.Value = Brushes.LightBlue; } else { backGroundSetter.Value = Brushes.Beige; } st.Setters.Add(backGroundSetter); return st; } }
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.
<ListView
ItemsSource="{Binding Source={StaticResource EmployeeData},
XPath=Employee}"
ItemContainerStyleSelector="{DynamicResource myStyleSelector}" >
<ListView.View>
<GridView>
<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"/>
</GridView>
</ListView.View>
</ListView>
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.
Available since 3.0
Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

