RadControls for WPF

What is a Style Selector?

The StyleSelector provides a way to apply styles based on custom logic.

CopyC#
public class StyleSelector
CopyVB.NET (Declaration)
Public Class StyleSelector
CopyVB.NET (Usage)
Dim instance As StyleSelector
CopyXAML
<StyleSelector .../>

Typically, you use a style selector when you have more than one styles defined for the same type of objects. For example, use it if your binding source is a list of student objects and you want to apply a particular style to the part-time students. You can do this by creating a class that inherits from StyleSelector and by overriding the SelectStyle() method. Once your class is defined you can assign an instance of the class to the style selector property of your element.

For more information read further in the topic.

How to Create a Style Selector?

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

SelectStyle() method.

CopyC#
public virtual Style SelectStyle( object item, DependencyObject container );

CopyVB.NET
Public Overridable Function SelectStyle ( _
    item As Object, _
    container As DependencyObject _
) As Style

Where the SelectStyle() method accepts the following parameters:

  • item - the data object for which to select the style.
  • container - the data-bound object.

The method returns a Style or a null reference (Nothing in Visual Basic). The default value is a null reference (Nothing in Visual Basic).

The following example shows how to define a StyleSelector.

In this example, the binding source is a list of League objects. Each League object has a list of Division objects. Each Division object has a list of Team objects.

CopyC#
public class SampleStyleSelector : StyleSelector
{
    public override Style SelectStyle( object item, DependencyObject container )
    {
        if ( item is League )
            return this.LeagueStyle;
        else if ( item is Division )
            return this.DivisionStyle;
        else if ( item is Team )
            return this.TeamStyle;
        return null;
    }
    public Style LeagueStyle
    {
        get;
        set;
    }
    public Style DivisionStyle
    {
        get;
        set;
    }
    public Style TeamStyle
    {
        get;
        set;
    }
}
CopyVB.NET
Public Class SampleStyleSelector
    Inherits StyleSelector

    Public Overloads Overrides Function SelectStyle(ByVal item As Object, ByVal container As DependencyObject) As Style
        If TypeOf item Is League Then
            Return Me.LeagueStyle
        ElseIf TypeOf item Is Division Then
            Return Me.DivisionStyle
        ElseIf TypeOf item Is Team Then
            Return Me.TeamStyle
        End If

        Return Nothing
    End Function

Private _LeagueStyle As Style
    Public Property LeagueStyle() As Style
        Get
            Return _LeagueStyle
        End Get
        Set(ByVal value As Style)
            _LeagueStyle = value
        End Set
    End Property

Private _DivisionStyle As Style
    Public Property DivisionStyle() As Style
        Get
            Return _DivisionStyle
        End Get
        Set(ByVal value As Style)
            _DivisionStyle = value
        End Set
    End Property

Private _TeamStyle As Style
    Public Property TeamStyle() As Style
        Get
            Return _TeamStyle
        End Get
        Set(ByVal value As Style)
            _TeamStyle = value
        End Set
    End Property
End Class

How to Apply a StyleSelector

How to Apply a StyleSelector

In order to be able to apply a style selector, you should first define a resource key for it.

CopyXAML
<example:SampleStyleSelector x:Key="myContainerStyleSelector"
    LeagueStyle="{StaticResource LeagueItemContainerStyle}"
    DivisionStyle="{StaticResource DivisionItemContainerStyle}"
    TeamStyle="{StaticResource TeamItemContainerStyle}"/>
Note

Note how the particular styles (LeagueStyle="{StaticResource LeagueItemContainetStyle}") are passed to the selector. If you want to use this approach you must declare the styles before the selector declaration.

Note

The example prefix maps to a CLR namespace and the corresponding assembly where the StyleSelector is defined. For more information see XAML Namespaces and Namespace Mapping.

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

CopyXAML
<telerikNavigation:RadTreeView ItemContainerStyleSelector="{StaticResource myContainerStyleSelector}"/>