Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StyleSelector and return style from XAML

Tags:

styles

wpf

I created a style in XAML, How Can I return this style in style selector (code)?

I created style in XAML and I want to only return the style which is declared in XAML.

like image 618
Never Avatar asked Nov 29 '22 15:11

Never


1 Answers

You can add a property to your StyleSelector and then use the property to pass a reference to the Style in XAML.

public class MyStyleSelector : StyleSelector
{
    private Style styleToUse;

    public Style StyleToUse
    {
        get { return styleToUse; }
        set { styleToUse = value; }
    }

    public override Style SelectStyle(object item, DependencyObject container)
    {
        return styleToUse;
    }
}

<Control StyleSelector="{DynamicResource myStyleSelector}">
    <Control.Resources>
        <Style x:Key="myStyle">
        ...
        </Style>
        <local:MyStyleSelector x:Key="myStyleSelector" StyleToUse="{StaticResource myStyle}"/>
    </Control.Resources>
</Control>
like image 100
Jiří Skála Avatar answered Dec 11 '22 15:12

Jiří Skála