0

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.

aromore
  • 977
  • 2
  • 10
  • 25

1 Answers1

2

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>
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • its a file consisting of numerous styles...is there a way i can reference it inside ? – aromore Feb 24 '14 at 17:13
  • 1
    Yeah you can do that using ResourceDictionary. I have added links in answer which will get you going. – Rohit Vats Feb 24 '14 at 17:16
  • I have done exactly this, but when I remove the style from individual user controls after declaring the style at the container window.resources level, the controls inside the user control cannot find the style, that is the style is not cascading through... That is why i posted this qn, i felt i was not referencing it right in the window level. – aromore Feb 24 '14 at 17:39
  • Make sure you have added complete RelativePath of resource file as a Source path. It should be `FolderName\ResourceFile.xaml` Can you post relevant code sample here? – Rohit Vats Feb 24 '14 at 17:44
  • I found the issue...i was adding the x:Key for the controls in my styles file. so, it kept looking for the implicit style for the control, and couldn't find one. i removed that and assigned the targetType, and it works like a wonder now! :) – aromore Feb 24 '14 at 18:08