StyleSelector Class

Provides a way to apply styles based on custom logic.

System.Object
  System.Windows.Controls.StyleSelector

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

type StyleSelector =  class end
<StyleSelector .../>

The StyleSelector type exposes the following members.

  NameDescription
Public methodStyleSelectorInitializes a new instance of a StyleSelector.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSelectStyleWhen overridden in a derived class, returns a Style based on custom logic.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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.

No code example is currently available or this language may not be supported.

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.

.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft