How can I load a WPF theme? - wpf

My understanding of the difference between a WPF theme and a WPF skin is the following:
A WPF skin is a set of resources loaded by an application.
A WPF theme is a set of resources handled by the OS.
To load a skin, I can just call Application.Current.Resources.MergedDictionaries.Add (mySkin);
However, I don't see any way to load a theme.
Is this documented or available?
Should I access the System.Windows.SystemResources internal class?

You can load them as a ResourceDictionary:
<Window
x:Class=”TestProject.Window1?
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”>
<Window.Resources>
<ResourceDictionary
Source=”/presentationframework.aero;component/themes/aero.normalcolor.xaml” />
</Window.Resources>
</Window>
Note: You would need to have a reference to the PresentationFramework.Aero.dll.

There's quite a subtle difference between Skins and Themes, and the reason why you're having problems with what you're trying to do might stem from this:
In WPF, a theming and skinning takes
on slight variations to their
meanings. Theming refers to
controlling the look and consistency
of an application UI to match the
operating system. For example, a WPF
application can be themed for the
Windows Aero theme or the Windows
Classic Theme. Skinning refers to
changing the application's appearance.
In other words, applying or letting
the user pick a skin to change the
look and feel of the application.
Robby Ingrebertsen, while working on
the WPF team, simplifies it as
follows:
Around here, we generally say that "theming" refers to the system theme
and "skinning" refers to changes to a specific app. This has helped to
clarify our internal communication
From here
So essentially, if you want your app to look like one of the Windows themes,ie the current windows theme - you don't have to set any styles in your app and it'll chose a pre-defined XAML skin that resembles it automatically. But, if you want to style your application, you make a skin for the app as you're doing.
As far as loading the Windows themes, this answer might help

(Answering my own question)
The way to load a resource dictionary as a theme is to add it to the list of merged dictionaries of the generic.xaml resource dictionary.

Related

How do I apply the current Windows theme in my WPF application?

I am working on an WPF application. WPF allows me to style everything but I just want my app to have the same theme as the other Windows applications. I want it to use the current Windows theme. Is this possible?
Whilst you're doing your styling you will be using brushes and colors.
If you want your styling to conform to the windows theme then you should ensure all your brushes and colors used are based on SystemColors.
https://learn.microsoft.com/en-us/dotnet/desktop/wpf/graphics-multimedia/how-to-paint-an-area-with-a-system-brush?view=netframeworkdesktop-4.8
https://learn.microsoft.com/en-us/dotnet/api/system.windows.systemcolors?view=netcore-3.1
You will find that a number of the default templates do not do this and instead have "hard coded" colors. Hence you will have to provide your own replacement templates for all these.
Unless you have extensive experience of wpf templating you are likely under estimating the amount of work which will be involved.
This is a lot of work.
If you look at custom themes which are available you'll often find the author missed some subtle aspects of controls here and there.

WPF: Windows 7 vs Windows 10 Appearance

I have annoying appearance differences between Windows 10 devices and Windows 7 devices.
I am using WindowStyle="None" and DockPanel directly inside my Window element.
What I don't get is why there is still a border? Why are the borders on buttons, textboxes, comboboxes, etc. rounded?
It seems this is related to Aero. Is there a way I can stop my application from using Aero? I'm assuming there is another presentation framework related to Windows 10 but do not know what it is called to force it.
Would a BorderBrush be the easiest way to resolve this?
<Window x:Class="CBD.Presentation.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CBD.Presentation"
Title="CBD" Height="760" Width="944" WindowStartupLocation="CenterScreen"
WindowStyle="None" SizeChanged="Window_SizeChanged" MinWidth="944" MinHeight="760"
Icon="favicon.ico">
<DockPanel x:Name="Root_Window" Background="Black">
<!--Application stuff here -->
</DockPanel>
</Window>
Windows 7 display:
Windows 10 display:
Everything still functions properly but the way some textboxes and buttons are setup, parts of letters are missing.
I think both these is something a lot of people are going to stumble upon when they really start customizing the style of their applications. There's some well established shortcuts to both problems, such as using existing style/libraries for WPF:
There's a few common Window and style libraries I see:
MahApps.Metro is a great library I've used for a long time for getting clean windows and interfaces (especially with 'dark' themes some customers love).
Modern UI. Little personal experience, but it's similar in that it has more modern window designs, as well as a big style set. I've seen it used frequently.
Elysium. Again, not much experience, but seems active. I've not seen it used as much as the other two.
These, of course, include a lot of other Metro theming. There are other theme packs available, I've found the Material Design In XAML Toolkit great if a client want's a modern design (Also recognizable, being from Google).
It looks like you're making your style from scratch, but the above might still be useful for you to simply use for the windows. If you want to go at it yourself there's a lengthy discussion with lot's of answers here.
For the curved corners, as discussed in the comments, this is as a result of WPF's default behavior on different operating systems. It attempts to select a default style to blend in best with the current operating system. For most applications that just use the default theme, this doesn't often matter.
When you're styling everything yourself though, it really plays havoc with what you have manually configured, because some properties that work well with the theme you set things up on might be different - like the rounded corners.
There's two options.
Explicitly set all the properties, so it looks exactly how you want it. Doing this means that even if the 'base' theme changes, it will still look good. This is how those libraries above do things - they define a style completely. Problem is you will have to test it on the different themes manually and it's a bit of work.
Manually set the base theme. (Can also be used for testing in option 1!) You can override which theme your app uses manually rather than letting it use the default for the operating system in much the same way you apply other custom themes, or maybe your own:
<App.Resources>
<ResourceDictionary Source="/PresentationFramework.Aero, Version=3.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35,
ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml"/>
</App.Resources>
For some reason the above would not appear as code unless encased in quotes...

How do I access to the resources in the Fluent Ribbon Control Suite?

I'm developing a WPF application that uses the Fluent Ribbon Control Suite and i'd like to use its 'themes' in the rest of the application.
Specifically, when you choose to use the Blue theme
<ResourceDictionary Source="pack://application:,,,/Fluent;Component/Themes/Office2010/Blue.xaml" />
I would like to use some of the colours inside that file, to style other interface elements (buttons, popups etc).
Any idea how I might do this?
You can try this, but it's not ideal…
Downloaded the Source Code from here and open it up in Blend.
Using the Resources tab, browse the different ResourceDictionary file (ColorsBlue.xaml, Button.xaml etc.)
Make a note of the resource keys for the resources you want.
In your original application add the styles/colours etc you want.
Now whenever you change the theme (Blue/Black/Silver etc) it will automatically change the background colour of your controls!
Hope that helps.

How do WPF themes get loaded?

WPF controls get their default styles, colors and brushes from a theme (Usually, in PresentationFramework.Aero.dll).
What piece of loads this assembly? And where are the resource dictionary stored?
I have my own WPF custom themes and load them in the Application.Current.Resources.MergedDictionary.
However, this does not work if the WPF themed control is hosted in Windows Forms since Application.Current is null.
Is there a way to do something similar to what WPF does? If so, what is it?
You cannot use the WPF themes in Winforms because they are two different technologies. There isn't a similar theme mechanism in Winforms natively. You can use third-party controls that support themes in Winforms. The most notable of these are the tools from Infragistics and Telerik.
Bottom line answer is no, there isn't.
Can you try just adding the theme to the control instead of the Application?
control.Resources.MergedDictionaries.Clear();
control.Resources.MergedDictionaries.Add(resourceDictionary);
(answering my own question)
The way to load a resource dictionary as a theme is to add it to the list of merged dictionaries of the generic.xaml resource dictionary.
There is no other way to load a resource dictionary as a theme.
This works fine when used from WinForms

Resource Dictionaries in a Silverlight Assembly?

I've just begun dabbling in putting together a set of controls as assemblies and I'm working on default styling. What I currently have is a UserControl in a project (thanks Reed!) and I'm able to bring that into another project via reference. I plan to add more controls over time to build something of an SDK.
I currently have some hooks that look for resources in the hosting application which either apply the resources to their respective properties, or style out the control via hard coded defaults.
Is it possible to set up resource dictionaries within the project containing the UserControls so they can use those references as the default, instead of hard coding? If so, how do I target them?
(I have a ResourceDictionary set up within the same project as the controls: Resources>Dictionaries>Colors.xaml)
Thanks in advance!
E
You should really look at creating custom templated controls in library rather than derivatives of UserControls. This will allow projects that reference your library to specify an alternative default style for you controls in the same way as we can for the controls in Microsofts own SDK.
The Creating a New Control by Creating a ControlTemplate topic on MSDN is good starter.
I think this is a better explanation, but i'm trying on a desktop application and i got the same problem.
XamlParseException: Failed to create a 'System.Type' from the text 'local:CustomerEntity'
If I'm undestanding correctly you want to create the file "generic.xaml" in the folder "Themes". However, I don't believe automatic styling works with UserControl only with Control. Generally if you trying to make a control that can be stylized and retemplated you want to inherit from Control and not UserControl.

Resources