Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9006069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:18:55+00:00 2026-06-16T01:18:55+00:00

I have a couple different applications where this would be helpful, but for the

  • 0

I have a couple different applications where this would be helpful, but for the sake of a concrete example, let us suppose I have a WinForms application.

This application makes use of WPF controls within ElementHost objects. I now would like to define an implicit style for all WPF Buttons (System.Windows.Controls.Button) so that every ElementHost does not need to merge in the resource dictionary nor does every Button need to explicitly specify the style.

Where do I define said style?

I have tried creating the resource dictionary Themes\Generic.xaml in the project root and specifying

[assembly: ThemeInfo(
    ResourceDictionaryLocation.None,
    ResourceDictionaryLocation.SourceAssembly )]

in the AssemblyInfo.cs. This did not work and I am under the impression that that styles there are only for custom controls defined in the same assembly, where as Button is defined in a foreign assembly.

Examples place implicit style code in <Application.Resources>; however, that node is with-in App.xaml, which (the project not having started its life as a WPF application) does not exist. Is it possible to add an App.xaml or is there some other place to put <Application.Resources> such that they are recognized?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:18:56+00:00Added an answer on June 16, 2026 at 1:18 am

    Dr WPF has several suggestions:
    http://drwpf.com/blog/2007/10/05/managing-application-resources-when-wpf-is-hosted/

    Some relevant parts quoted below:


    Create an Application Instance and Add Resources in Code
    Below is a very simple function that will create the Application object if it does not exist and then load some resources:

    public static void EnsureApplicationResources()
    {
        if (Application.Current == null)
        {
            // create the Application object
            new Application();
    
            // merge in your application resources
            Application.Current.Resources.MergedDictionaries.Add(
                Application.LoadComponent(
                   new Uri("MyLibrary;component/Resources/MyResourceDictionary.xaml",
                   UriKind.Relative)) as ResourceDictionary);
        }
    }
    

    Now you just need to make sure that you call this function prior to parsing any XAML files that contain static resource references to application-level resources. To do this, simply add a call to the above function in the constructor of your markup-based classes before any call to InitializeComponent():

    public Page1()
    {
        EnsureApplicationResources();
        InitializeComponent();
    }
    

    Define the Application Class in XAML and Create It on the Fly
    First, we do not want MSBuild to generate an application entry point for our Application class. So instead of declaring the App.xaml file as an ApplicationDefinition element in the project file, we need to declare it as a Page element:

    <Page Include="App.xaml" />
    <Compile Include="App.xaml.cs">
      <DependentUpon>App.xaml</DependentUpon>
      <SubType>Code</SubType>
    </Compile>
    

    Next, we need to make sure that our App.xaml markup is parsed. Typically, this is done as part of the entry point function (which we just eliminated). Instead, we can simply define a constructor for the Application class and call InitializeComponent directly:

    public App()
    {
        InitializeComponent();
    }
    

    Now all our resources and merged dictionaries can be declared in App.xaml and our static function to load the Application instance can be as simple as this:

    public static void EnsureApplicationResources()
    {
        if (Application.Current == null)
        {
            // create the Application object
            new App();
        }
    }
    

    Manage a Collection of Resource Dictionaries in Code and Merge them at the Element Level
    In this scenario, we do not leverage an Application object at all. Instead, we dynamically load each ResourceDictionary at runtime and selectively merge it into pages or windows or specific elements, as necessary.

    public static class SharedResources
    {
        public static readonly DependencyProperty MergedDictionariesProperty =
            DependencyProperty.RegisterAttached("MergedDictionaries",
                typeof(string), typeof(SharedResources),
                new FrameworkPropertyMetadata((string)null,
                    new PropertyChangedCallback(OnMergedDictionariesChanged)));
    
        public static string GetMergedDictionaries(DependencyObject d)
        {
            return (string)d.GetValue(MergedDictionariesProperty);
        }
    
        public static void SetMergedDictionaries(DependencyObject d, string value)
        {
            d.SetValue(MergedDictionariesProperty, value);
        }
    
        private static void OnMergedDictionariesChanged(DependencyObject d,
            DependencyPropertyChangedEventArgs e)
        {
            if (!string.IsNullOrEmpty(e.NewValue as string))
            {
                foreach (string dictionaryName in (e.NewValue as string).Split(';'))
                {
                    ResourceDictionary dictionary = GetResourceDictionary(dictionaryName);
                    if (dictionary != null)
                    {
                        if (d is FrameworkElement)
                        {
                            (d as FrameworkElement).Resources
                                .MergedDictionaries.Add(dictionary);
                        }
                        else if (d is FrameworkContentElement)
                        {
                            (d as FrameworkContentElement).Resources
                                .MergedDictionaries.Add(dictionary);
                        }
                    }
                }
            }
        }
    
        private static ResourceDictionary GetResourceDictionary(string dictionaryName)
        {
            ResourceDictionary result = null;
            if (_sharedDictionaries.ContainsKey(dictionaryName))
            {
                result = _sharedDictionaries[dictionaryName].Target;
            }
            if (result == null)
            {
                string assemblyName = System.IO.Path.GetFileNameWithoutExtension(
                    Assembly.GetExecutingAssembly().ManifestModule.Name);
                result = Application.LoadComponent(new Uri(assemblyName
                    + ";component/Resources/" + dictionaryName + ".xaml",
                    UriKind.Relative)) as ResourceDictionary;
                _sharedDictionaries[dictionaryName] = new WeakReference(result);
            }
            return result;
        }
    
        private static Dictionary<string, WeakReference> _sharedDictionaries
            = new Dictionary<string, WeakReference>();
    }
    

    This would allow us to merge in shared resource dictionaries to the Resources collection of any framework element, by simply doing this:

    <Grid dw:SharedResources.MergedDictionaries="ApplicationBrushes;ButtonStyles">
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a application that generates a couple of different mails. These mails are
I would like to access a couple of different Mac OS X applications from
I have a couple of SQL servers with databases supporting two different applications. I
I have found a couple of similar questions, but I've got a slightly different
We have couple different web-apps that share the same db link. The hibernate layer
I have a couple different apps that write text files to the Documents Directory
I have a worked on a couple different projects and I have seen two
I have a couple of projects with different release cycles sitting in my SVN
I have some code in a couple of different functions that looks something like
I have a PHP page that does a couple of different things depending on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.