WPF Themes not applying to background - wpf

I have a WPF application I am developing, that allows the user to switch the current theme. I figured out how to switch themes... but it appears that the background of the application isn't affected by the theme:
(I blended three pictures together to conserve space)
How can I fix this? It clearly shows here that the background is supposed to change...
Here's the code I'm using:
MainWindow.xaml:
<Window x:Class="GDE.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" MinWidth="525" MinHeight="350">
<Grid>
....
</Grid>
</Window>
App.xaml:
<Application x:Class="GDE.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary x:Name="ThemeDictionary">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Themes/ExpressionDark.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>

The themes define a background but you need to set it up yourself since it is not being referenced anywhere within the theme:
Background="{DynamicResource WindowBackgroundBrush}"
Why? I'd assume because styles are not automatically applied to derived classes, so if a style with theTargetType Window is set up that would also need to be set explicity since normally you use a subclass of Window, e.g. MainWindow.

My guess is that that the background we see in your screenshot does not belong to one of the themes, and this is the problem, you override theming when setting stuff manually.

Related

Moving merged XAML ResourceDictionaries to Generic.xaml causes error

I have created two custom controls, both inherited from System.Windows.Controls.Button. One is called XLButton and the other is XLBox. They have identical XAML styles/templates in two separate ResourceDictionary objects in two separate .xaml files, and identical code-behind files, except that "XLButton" appears in the XLButton files where "XLBox" appears in the XLBox files, and vice versa.
I have created a simple test window with a two-row Grid. I merge the the ResourceDictionary files into a the Window.Resources of that test window and create an instance of each custom control, one in the top row, one in the bottom. This works fine. Here's the test window's XAML:
<Window x:Class="ScratchPadWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ExLuminaControls"
mc:Ignorable="d"
Title="ScratchPadWindow" Height="118" Width="145">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Styles/XLBox.xaml" />
<ResourceDictionary Source="/Styles/XLButton.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<local:XLBox Content="Hi!"/>
<local:XLButton Grid.Row="1" Content="Yeah!"/>
</Grid>
</Window>
This works just fine. But, when I comment out the ResourceDictionary.MergedDictionaries section and copy it in its original form to Themes\Generic.xaml, so it looks like this:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ExLuminaControls"
mc:Ignorable="d">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Styles/XLButton.xaml" />
<ResourceDictionary Source="/Styles/XLBox.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
I get a "Cannot locate resource 'styles/xlbutton.xaml' error, associated with this line:
<local:XLBox Content="Hi!"/>
That doesn't make sense to me, but what's more confusing is that the problem goes away if i click "Disable project code" in the designer.
I'm using Blend 2017 Community.
Can anyone help me understand this?
Thanks!
it's path problem, when in generic, use it like this
<ResourceDictionary Source="/AssemblyName;Component/Styles/XLButton.xaml" />
or have to go up with "../../"
themes and generic lowercase
assembly info
app.xaml if nothing works
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Fluent;Component/Themes/Generic.xaml" />
...
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Definition and style
#region --------------------CONSTRUCTORS--------------------
static WaitSpin()
{
FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(WaitSpin),
new FrameworkPropertyMetadata(typeof(WaitSpin)));
}
/// <summary>
/// LoadingAnimation constructor.
/// </summary>
public WaitSpin()
{
this.DefaultStyleKey = typeof(WaitSpin);
}
#endregion
<Style x:Key="{x:Type local:WaitSpin}" TargetType="{x:Type local:WaitSpin}">

Cannot instantiate UserControl from another assembly - Resource cannot be found

I have a solution with two WPF projects: Primary and Secondary.
In the Primary project, a Window named PrimaryView instantiates an UserControl called SecondaryControl, defined in Secondary project.
SecondaryControl uses SecondaryStyle, which is defined in SecondaryResourceDictionary (as you might have guessed already, defined in SecondaryProject).
The fact is: when I try to run the solution, I get a XamlParseError, and digging InnerExceptions I eventually find the culprit, the ResourceNotFound error.
So my questions are:
If SecondaryControl and its SecondaryStyle are defined in the same assembly, why can't I instantiate it it PrimaryAssembly?
Should I make SecondaryStyle available to PrimaryProject namespace somehow? Why?
I try to help you by explanation how it works in my projects.
A separate assembly contains a common control and common resource dictionary like this:
CommonResources.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!-- Some resources -->
</ResourceDictionary>
SomeCommonControl.xaml
<UserControl x:Class="YourAssembly.SomeCommonControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/YourAssembly;component/CommonResources.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<!-- Some specific content -->
</UserControl>
I can use this control and resources from another assemblies and WPF-projects like this:
<Window x:Class="YourWPFProject.SomeWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:common="clr-namespace:YourAssembly">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/YourAssembly;component/CommonResources.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<common:SomeCommonControl />
</Window>
I hope this will help you.

WPF how to force designer to display custom window style

I made a new CustomControl based on the Window Control.
When I use my Control it doesn't appear in the designer mode, instead it still uses the default window style.
How can I force the designer to display my window style instead of the default one?
My MainWindow.xaml:
<CustomWindow:MetroWindow x:Class="Testz.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:CustomWindow="clr-namespace:MetroWindow;assembly=MetroWindow"
Title="MainWindow" Height="350" Width="525" BorderBrush="Red">
<Grid>
</Grid>
</CustomWindow:MetroWindow>
Link to my whole project - maybe you'll need it
How it looks in the designer and how it really looks:
I think I understood what you was trying to accomplish.
The problem is that the Visual Studio Designer can't find the Resource because it is on the library. What you need to do is to create a ResourceDictionary pointing to it on you Application to be able to see the designer time template.
<Application x:Class="DemoMetroWindow.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MetroWindow"
StartupUri="DemoWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:/MetroWindow;component/Themes/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
You can learn more from links bellow.
OnApplyTemplate() never being called
WPF get Type in Design time?
http://blogs.msdn.com/b/jgalasyn/archive/2007/10/29/troubleshooting-wpf-designer-load-failures.aspx
http://blogs.msdn.com/b/jnak/archive/2007/11/08/code-behind-and-the-wpf-designer.aspx
You're using Mahapps Metro, right?
You can use the styles provided by it.
Styling a window with Metro
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colours.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
You can change the color of the window by changing the Resource dictionary of Blue.xaml by other colors, just check it out.
When the resource references in App.xaml are fine you should restart Visual Studio. In most cases the themes are then displayed correctly.
Regards

MahApps.Metro: Disabling windows animation

I'm using MahApps.Metro UI for my WPF application. It's a good one and satisfies my needs, but I'd be more happy if somebody told me how to disable windows animation when they pop up.
When I call the Show() method, the new window pops up and I see an annoying animation (the content slides from right to left). The effect is similar to another one shown on the picture below (but it shows tabs and content goes left-to-right):
Sample of a dummy form please see below:
<controls:MetroWindow x:Class="TestProj.Views.TestView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:vm="clr-namespace:TestProj.ViewsModels"
Height="230" Width="550">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colours.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
</Grid>
</controls:MetroWindow>
Set WindowTransitionsEnabled="False" in xaml of the window.
As discussed on the equivalent GitHUb issue, the MetroWindow control template by default will use a MetroContentControl (which has this animation).
You need to edit the template to change it back to a ContentControl.
Sample code here

How to add controls on dialog which has predefind WPF theme?

I am new to WPF. I want to do something like this:
I have multiple dialogs in an application. I have created one theme with the required background, title bar and close button I want for all dialogs. Whereas, all dialogs size, and controls for them will differ.
For example: DialogWindow is a theme I have created for Window control.
On MainWindow (where I apply this DialogTheme), I can see this theme.
But when I try to add controls on it, they do not show up on the theme.
<Window x:Class="Example.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="391" Width="616"
Style="{DynamicResource DialogWindow}">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Themes/DialogsTheme.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
</Window>
I am not sure, what is going wrong. Any help will be appreciated.
You need to add these lines to each window you create to get achieve the style...
<Window...Style="{DynamicResource DialogWindow}">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Themes/DialogsTheme.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
Finally got an answer, ContentPresenter is a thing required, which works as client area for window theme.
<ContentPresenter Grid.Row="1" Grid.ColumnSpan="2" x:Name="ClientArea" />

Resources