
Based on the sample from MetroStyleWindow, I know we can add a custom button to the title bar.
However, I could not figure out how to set the size of the button so that it will work for different Themes (metro, classic, etc).
Based on the sample from MetroStyleWindow, I know we can add a custom button to the title bar.
However, I could not figure out how to set the size of the button so that it will work for different Themes (metro, classic, etc).
Never mind. I figured out how to do that in code behind.
Hello,
We had defined our DataTemplates for the Button.ContentTemplate as resources. Classic buttons would use DataTemplates whose Canvas was 12x10 size, while Metro buttons would use DataTemplates whose Canvas was 42x26 size, and Aero buttons would use DataTemplates whose Canvas was 27x13 size.
We defined these DataTemplates using the same keys (i.e. themes:SharedResourceKeys.WindowTitleBarButtonCloseGlyphTemplateKey) in separate XAML files: WindowTitleBarButtonGlyphs.Classic.xaml, etc.
As long as you use a DynamicResource reference to the key in your Button.ContentTemplate property, it should resolve based on the appropriate one if you set up your app's Themes folder to have Generic.xaml (Aero), Aero2.NormalColor.xaml (Metro), AeroLite.NormalColor.xaml (Metro), and Classic.xaml (Classic) files that include the appropriate WindowTitleBarButtonGlyphs XAML file.
You may also need this attribute in your app's AssemblyInfo to tell WPF to pull those resources based on system theme:
This MSDN topic talks about that attribute and Themes files a bit:
https://docs.microsoft.com/en-us/dotnet/api/system.windows.themeinfoattribute?view=netframework-4.8
The above is the "proper" way to do it but if your code-behind approach works, then that's fine too.
Thank you very much for providing the proper way to do this.
I am able to get it to work for the Metro themes and the classic theme but not the Aero, High Contrast and Office themes.
All Metro themes are affected by "Aero2.NormalColor.xaml"
Classic theme is by "Classic.xaml"
"Generic.xaml" has no effect on Aero theme.
What file names to use for Aero, High Contrast and Office themes?
Thank you!
Hi Rex,
In our products, the XAML theme files we set up that are auto-recognized by WPF's ThemeInfoAttribute based on current Windows system theme are:
The link in our previous reply for ThemeInfoAttribute references most of them other than the newer Aero2 and AeroLite ones, both of which should go to Metro-like themes.
I don't believe there is one for high contrast. You have to detect high contrast mode on your own and switch to an appropriate theme programmatically when found. There is no Windows system theme for Office-style, so there are no options for auto-selecting those.
Thank you very much. I guess there is no completely clean way of doing this without code behind.
Please log in to a validated account to post comments.