Search code examples
.netwpfc#-4.0implicit-style

Implicit styling of a container window


I am developing an application, having a window container consisting of child user controls. I have a MyStyles.xaml document which is being used by individual user controls and their children at the control level.

That is, I am declaring the styles inside of each user control, and applying it to individual controls using

<Combobox Style = {StaticResource MyStylesComboBox} ...

How can i make this implicit, by defining the style for the user controls at the container-window level, so that the style cascades down the hierarchy?

Thanks in advance.


Solution

  • Declare the styles under Window resources(in case want to be share across UserControls hosted under this window)

    OR

    may be under App resources (in case want to share across multiple windows)

    <Window.Resources>
       <!-- Move your styles here -->
    </Window.Resources>
    

    In case it's a file you can merge it using ResourceDictionary. Have a look at this for more detail Creating and Consuming ResourceDictionary.

    Something like this:

    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="ResourceFileName.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>