How to get Themes working inside an ElementHost (FormsIntegration)

WPF Studio, Themes, and Shared Library for WPF Forum

Posted 12 years ago by keepITcool - Amsterdam
Version: 12.1.0560
Avatar

I am building an Office Addin using WPF for my usercontrols.  I interact with user via CustomTaskPane so I have to use a Forms.Usercontrol with a FormsIntegration.ElementHost which hosts the WPF

My problem:   CurrentTheme changes are not reflected on my UI.. and I've been trying and typing and cursing a lot over the past days.

When I ran the same control directly in a WPF Window everything works as expected... which is when I realized that apparently the ElementHost is interfering somehow.  I can partly solve it using an eventhandler for CurrentTheme which uses SetTheme on individual controls, but that's error prone and cumbersome.

Anyone know how to solve this the correct way?

Regards,

Jurgen

Example code can be supplied in a simplified VB.project

Comments (5)

Answer - Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Jurgen,

Our themes just set the App.Resources collection when you set the ThemeManager.CurrentTheme property.  Perhaps when used in an ElementHost, the Application object never instantiates an Application.Current value though since it's not a WPF app.  And if that is the case, I'm not sure if you can manually create one either.

You might be able to use the ThemeManager.SetTheme method directly on your root WPF control in the ElementHost, and have it work that way.  If you have more than one ElementHost though, I'd imagine that you'd need to do that in each root WPF control in an ElementHost.  It's a pain but there's probably no other way if there is no current Application for them to inherit resources from.


Actipro Software Support

Posted 12 years ago by keepITcool - Amsterdam
Avatar

Thanks for that reply. I've been working with WPF since a few months only..

I have it working now.. BUT with some caveats..

1) Updating the ThemeManager must be done at the very start of my code, before creating any UI elements.
     AFAIK  Office doesnot expose Theme property nor ThemeChange event but I read it from the registry and statically set the my UI theme.

2) Standard office themes appear to have no effect BUT custom themes based on standard officethemes work correctly. 
No problem as I want to control Tint anyway.

    ThemesOfficeThemeCatalogRegistrar.Register()
    ThemeManager.RegisterThemeCatalog(New TintedThemeCatalog("MyOfficeBlue", ThemeName.OfficeBlue.ToString, Colors.SteelBlue))
    ThemeManager.RegisterThemeCatalog(New TintedThemeCatalog("MyOfficeBlack", ThemeName.OfficeBlack.ToString(), Colors.Black))

3)  Setting AreNativeThemesEnabled in my initializer appears to have no effect BUT works fine when my usercontrol.xaml sets it with markup.

4)  Scrollbar default style override created in my own resourceDictionary. I still need to do some work on the Itemspresenter for combobox :-)

5)  I use SetTheme on the ElementHost.Child via an eventhandler...  and it inherits fine in the content.
     your answer made me realize I have to research FormsIntegration to see how ElementHost implements the Application object

Thanks

 

 


   

 



 


2) The standard office themes  

Posted 7 years ago by ANAYOCHI AKU
Avatar

hi, please kindly help. i av tried all possible best but i just can't change the theme in my wpf window to officetheme 2010.

 

here is my sample code below:

 

my:RibbonWindow x:Class="LearningPrismShell.Shell"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:py="clr-namespace:ActiproSoftware.Windows.Controls.Ribbon;assembly=ActiproSoftware.Ribbon.Wpf"
xmlns:my ="http://schemas.actiprosoftware.com/winfx/xaml/ribbon"
xmlns:prism="http://www.codeplex.com/prism"
xmlns:themes="http://schemas.actiprosoftware.com/winfx/xaml/themes"
themes:ThemeManager.Theme="Office2007Black"
xmlns:navigation="http://schemas.actiprosoftware.com/winfx/xaml/navigation"
Title="LearningPrism" Height="350" Width="800" WindowState="Maximized">

 

 

after running the application is just the same old window.....

 

please i need it a quick reply...thanks

Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

One thing we've seen when using WPF controls in WinForms apps is that you should also make sure a WPF Application is created.  Code like this will do it:

​if (System.Windows.Application.Current == null) {
    wpfApplication = new System.Windows.Application();
    wpfApplication.ShutdownMode = System.Windows.ShutdownMode.OnExplicitShutdown;
}

Store wpfApplication as a static variable.  Then in your final WinForms app shutdown code, call wpfApplication.Shutdown().

Maybe that will help?


Actipro Software Support

Posted 7 years ago by keepITcool - Amsterdam
Avatar

Also a ribbonwindow inside an elementhost may not be ideal...

Afaik Elementhost s/b used for controls, not windows as the window will be drawn and handled by Forms, not wpf. I've gotten pretty handy with Excel and WPF, but please note there are many many caveats. Biggest problem is running input elements (combo/textbox) in non-modal taskpanes or windows.

Jurgen

The latest build of this product (v24.1.1) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.