Setting a style for a static resource for a root control

I am trying to set the style of the root Control in my XAML to a StaticResource defined in an external ResourceDictionary , however I get an error:

The resource "MyControlStyle" could not be resolved.

 <UserControl ... > <UserControl.Resources> <ResourceDictionary Source="..\Styles\MyStyles.xaml" /> </UserControl.Resources> <UserControl.Style> <StaticResource ResourceKey="MyControlStyle"/> </UserControl.Style> </UserControl> 

Besides setting the style in the XAML file that my control uses, how can this be done? I would like to be able to set the style from UserControl so that I can see the style effects in the designer for UserControl .

+1
source share
1 answer

You are trying to make a reference to a ResourceDictionary, which is defined inside the control from a user control property (Style in this case). Resources work like a bubble, if resources are needed for management, then it searches for a container in its resources, if they are not found, then the search in the parent container resumes if it cannot be found in its parent object .... until the resources applications will not be found in system resources.

If you want to apply a resource to a user control, you must place it in the application resources (in the App.xaml file). Try it.

+1
source

Source: https://habr.com/ru/post/1488239/


All Articles