简体   繁体   中英

Custom control styles not working (via Generic.xaml)

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:

  • The Generic.xaml is in the Themes\\ folder within the root of my project
  • The Generic.xaml has a Build Action of Page .
  • The Generic.xaml uses the custom tool flag MSBuild:Compile .
  • The external library has the following line declared: [assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]

What I tried:

Sources

  • 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"

Application

  • Manually referencing the Generic.xaml in my application with all possible source declarations

However, 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM