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

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" />

Related

how Handy Control don't effect on my App theme?

I want to use PersianDatepicker in my WPF App. I use HandyControl For That. but when use it in my App All of My App themes changed.
I use this Tag in my App.xaml
<ResourceDictionary.MergedDictionaries >
<hc:ThemeResources />
<hc:Theme />
</ResourceDictionary.MergedDictionaries>
Now I don't want this package to affect the All of My App themes.
You can use it in a specific UserControl and add config in it's resource like this:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:hc="https://handyorg.github.io/handycontrol"
x:Name="UserControl">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<hc:Theme />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<StackPanel>
<hc:PersianDateTimePicker
Style="{StaticResource DateTimePickerExtendPersian}" />
</StackPanel>
</UserControl>

Opening new window in MVVM WPF The window is a window, not a user control

I really like the solution from GazTheDestroyer to open a new window in MVVM WPF. My problem is that the window I want to show is a window and windows are not allowed in styles.
I have the following lines in my ResourceDictionary:
<DataTemplate DataType="{x:Type vm:ConfigViewModel}">
<vw:WinOptionsSimu />
</DataTemplate>
In WinOptionsSimu.xaml :
<Window x:Class="SimuDMSDMI.View.WinOptionsSimu"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WinOptionsSimu" SizeToContent="WidthAndHeight"
WindowStartupLocation="CenterOwner">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/SimuDMSDMI;component/Ressources/DictMainWindow.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
...
</Grid>
</Window>
After building the project, the line
is blue underlined and the tooltip shows "Can't put a Window in a style".
How can I solve this ?
Thanks in advance

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

WPF Themes not applying to background

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.

Resources