Reputation: 703
I have several several styles and templates for both standard WPF controls and custom controls in an external custom control libary project. For each control, there's a ResourceDictionary
in my \Themes
folder, along with a Generic.xaml
which contains a merged dictionary of all the other XAML files.
I made sure that:
Generic.xaml
is in the Themes\
folder within the root of my projectGeneric.xaml
has a Build Action of Page
.Generic.xaml
uses the custom tool flag MSBuild:Compile
.[assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]
What I tried:
Source="MyControl.xaml"
Source="/MyLibrary;component/Themes/MyControl.xaml"
Source="/MyLibrary;component/Themes/MyControl.xaml"
Source="/pack://application:,,,/Themes/MyControl.xaml"
Source="/pack://application:,,,/MyLibrary;component/Themes/MyControl.xaml"
Generic.xaml
in my application with all possible source declarationsHowever, nothing works. The application stays completely unthemed, even though I can see that all dictionaries have been loaded when accessing Application.Current.Resources
.
It's also worth noting that most of my control themes contain additional dynamic resources (for colors etc.), which are loaded manually when the application starts.
It seems that I'm facing the exact same unanswered problem as the OP in this question here, however they state that manually referencing the Generic.xaml
works for them.
Upvotes: 2
Views: 1430
Reputation: 3
But the question is old, i had same problem and i found the solution here: https://stackoverflow.com/a/46562565/16722051
Only you need to do, add the magic method call named SetResourceReference in your constructor of your custom control.
UPDATE:
SetResourceReference will do looking for a style based on type in the resources tree. So if you set manualy the Style of your custom control this will override it.
The correct usage, subscribe the Loaded event, and call the SetResourceReference method when the custom control Style is still null.
This solution make your custom control to work if you manualy set the Style throught the parent control ItemContainerStlye dp and if you not setted, call the SetResourceReference and will find the Style in the Resource tree.
public MyCustomControl()
{
Loaded += (s, e) =>
{
if (Style == null)
SetResourceReference(StyleProperty, typeof(MyCustomControl));
};
}
Upvotes: 0
Reputation: 372
Do you defined following code in static constructor?
DefaultStyleKeyProperty.OverrideMetadata (typeof (YourCustomClass),new FrameworkPropertyMetadata (typeof (YourCustomClass)));
don't use x:key ="style_name" in resource dictionary
Upvotes: 1