Hi,
I'm trying to set a theme at runtime, but I see no changes in my UI. Here's what I'm trying.
switch ( theme ) { case 1: ThemeManager.CurrentTheme = ThemeManager.ThemeNameAero; break; case 2: ThemeManager.CurrentTheme = ThemeManager.ThemeNameLeaf; break; case 3: ThemeManager.CurrentTheme = ThemeManager.ThemeNameOnyx; break; case 4: ThemeManager.CurrentTheme = ThemeManager.ThemeNameOffice2k7Silver; break; default: throw new Exception(); }
But it doesn't seem to work. I've also tried getting the PrimativesXXX ResourceDictionary merged into the apps ResourceDictionary, but that doesn't work either.
The above code is in the App.xaml.cs.
Thanks
Andy
Hello Andy,
I have managed changing the current theme by creating a Style targeting the element you want to change the theme of. For example, I want to change the current theme of my XamDataGrid and I do the following:
string[ themes = ThemeManager.GetThemes();
Style myStyle = new Style();myStyle.TargetType = typeof(XamDataGrid);myStyle.Setters.Add(new Setter(XamDataGrid.ThemeProperty,themes[0]));xamDataGrid.Style = myStyle ;
Does this work in your case?
Actually you should not change the Theme in a style setter since depending on where you place the Style you can get into an invalid state since setting the Theme puts the theme resources into the control's Resources which could override the local style which had the Theme setter which removes the Theme setting and removes the resources.
The CurrentTheme of the ThemeManager is the theme that is used by controls on which you have set the Theme property to "[current]" (or ThemeManager.ThemeCurrentLiteral) so if you want to use that mechanism you have to be sure to set the Theme property as such on all the controls. With regards to putting resources into the application resources, the PrimitivesXXX resources just contain the resources for the classes in the Windows assembly so you would have to include all the resources. If you wanted to go this route then you might try using the static GetResourceSet method passing in the theme name and a grouping of "*" (or ThemeManager.AllGroupingsLiteral) and put that resource into your application's resource dictionary.
Ok... so if I want the Infragistics themes to apply to ALL controls, not just Infragistics controls, I should be putting the resources in the application resources, using the method you describe?
Ok, the grids and dock manager use the themes, but basic wpf controls don't, and it doesn't look like much is done to igEditors either. Here's my new code. Am I doing something wrong?
switch ( theme ) { case 1: res = ThemeManager.GetResourceSet( ThemeManager.ThemeNameAero, ThemeManager.AllGroupingsLiteral ); break; case 2: res = ThemeManager.GetResourceSet( ThemeManager.ThemeNameLunaOlive, ThemeManager.AllGroupingsLiteral ); break; case 3: res = ThemeManager.GetResourceSet( ThemeManager.ThemeNameOffice2k7Silver, ThemeManager.AllGroupingsLiteral ); break; case 4: res = ThemeManager.GetResourceSet( ThemeManager.ThemeNameOnyx, ThemeManager.AllGroupingsLiteral ); break; default: throw new Exception(); }
Resources.MergedDictionaries.Add( res );
The themes that we have written are for our controls only at this point. We have not created/provided styles for the controls/elements that Microsoft ships as part of the WPF framework. You may want to submit a suggestion that we provide this as well.