Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use the style defined in resource dictionary :

I have a style defined for listboxitem in a resource dictionary. I want to use this style in a cs file of listbox :

I am doing the below thing but it gives me null ,CustomListBoxItemStyle is a name of a key given to the style.

public class CustomListBox : ListBox
{  
    public CustomListBox()
    {
        this.ItemContainerStyle = Application.Current.Resources["CustomListBoxItemStyle"] as Style;
    }
}

There is no xaml for this.

How to achieve this?

like image 943
Malcolm Avatar asked Jan 26 '26 15:01

Malcolm


1 Answers

I have a style defined for listboxitem in a resource dictionary.

but is that resource dictionary merged into the application's resource dictionary. It doesn't happen automatically you need to include it in the App.xaml like this:-

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="MyResourceDictionary.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <!-- other resource defined directly in app xaml -->
    </ResourceDictionary>
</Application.Resources>
like image 131
AnthonyWJones Avatar answered Jan 29 '26 04:01

AnthonyWJones