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

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>

Related

MetroApps not finding ResourceDictionary 2.0.0-alpha0748

I'm looking at utilizing the MetroApps 2.0.0-alpha0748 from Nuget. I've pulled that into my project and started implementing.
I am utilizing this inside of an existing application and this window will be called from an addin inside of that application. So I do not own the application context. So I have not utilized the typical Application.xaml Shared Resources, I was hoping to address that here.
Everything builds correctly. Whenever I run, I get the error below.
<Controls:MetroWindow x:Class="FamilyBrowser.Browser.ApplicationView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:helper="clr-namespace:RevitFamilyBrowser.BrowserHelper"
xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
xmlns:local="clr-namespace:FamilyBrowser.Browser"
xmlns:vm="clr-namespace:FamilyBrowser.BrowserViewModel"
xmlns:view ="clr-namespace:FamilyBrowser.BrowserView"
xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
Title="Family Browser" Height="800" Width="800"
MinHeight="300" MinWidth="400" Closing="Window_Closing"
>
<Window.DataContext>
<vm:ViewModelMain/>
</Window.DataContext>
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Themes/dark.red.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
</Controls:MetroWindow>
MahApps Error
I downloaded the Code-Samples from MahApps.Metro and was able to build and run the HamburgerMenuNavigation example without any issues. I was also able to change the styles/themes example from dark.red to light.red to light.orange.xaml etc. without any issues to confirm that is was not a machine specific issue.
Update:
<Controls:MetroWindow x:Class="FamilyBrowser.Browser.ApplicationView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:helper="clr-namespace:FamilyBrowser.BrowserHelper"
xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
xmlns:local="clr-namespace:FamilyBrowser.Browser"
xmlns:vm="clr-namespace:FamilyBrowser.BrowserViewModel"
xmlns:view ="clr-namespace:FamilyBrowser.BrowserView"
xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
Title="Family Browser" Height="800" Width="800"
MinHeight="300" MinWidth="400" Closing="Window_Closing"
>
<Controls:MetroWindow.DataContext>
<vm:ViewModelMain/>
</Controls:MetroWindow.DataContext>
<Controls:MetroWindow.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Themes/Dark.Red.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Controls:MetroWindow.Resources>
<Grid>
</Grid>
</Controls:MetroWindow>
Update #2:
We identified this was an error with another .dll loading in the 1.* library of MahApps even though I was loading the 2.* alpha content.
the names are case sensitive. Please try Dark.Red.xaml. It should work with last alpha.
Happy coding
Tim
I took your snippet and put it into a new clean sample App. I just deleted the connection to the model as I don't have your model at hand.
Here is the result:
and here is the modified XAML:
<Controls:MetroWindow x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
Title="Family Browser"
Width="800"
Height="800"
MinWidth="400"
MinHeight="300">
<Controls:MetroWindow.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Themes/Dark.Red.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Controls:MetroWindow.Resources>
<Grid>
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="100"
Foreground="{DynamicResource MahApps.Brushes.Accent}"
Text="Hello World" />
</Grid>
</Controls:MetroWindow>
I think there must be something else wrong in your App. If you can upload the not running sample on Github I may have a look if I find the bug. Maybe a Style or StaticResource is wrong or the MahApps-DLL is not found.
Happy coding
Tim
Image to Solution
The original problem was because of other applications using MahApps while I was trying to use MahApps and loading both assemblies from different versions.
This could have been more quickly grasped by navigating to Debug > Windows > Modules to determine there were two different assemblies.

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

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

Accessing resources during initialization of control

lets assume we have the following control definition
<ctrl:ChildWindow x:Class="Control.Editor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ctrl="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
xmlns:local="clr-namespace:Control"
Width="400" Height="300"
local:AttachedProperties.DialogResult="{Binding Path=DialogResult}"
Title="{Binding Path=Caption}" Style="{StaticResource Title}" DataContext="{Binding}" HasCloseButton="False">
<ctrl:ChildWindow.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Control;component/Resources/BaseAppearance.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</ctrl:ChildWindow.Resources>
</ctrl:ChildWindow&gt
The problem is that the style on the root control cannot be set because the ResourceDictionary is not loaded.
How can I get access to the StaticResource Title during the initialization of the control, when I don't have access to the App class? I'm also not sure that it would be possible, if I would have access to it.
Regards
I'd recommend accessing your resource and doing the work in the .Loaded() event of your control.
Edit: On second thought... I think I know what you're doing now... You have a resource set in your App.xaml class, but you want to access it in your control.
Easy way around the problem is to set it to a DynamicResource instead... but this is less performant.
What is the BuildAction set to on your App.xaml in the property's tab?
If it is ApplicationDefinition... then you should be able to access your resource as you currently are.
I found the common way without using code behind. I knew it is possible. ^^
<ctrl:ChildWindow x:Class="Control.Editor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ctrl="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
xmlns:local="clr-namespace:Control"
Width="400" Height="300"
local:AttachedProperties.DialogResult="{Binding Path=DialogResult}"
Title="{Binding Path=Caption}" DataContext="{Binding}" HasCloseButton="False">
<ctrl:ChildWindow.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Control;component/Resources/BaseAppearance.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</ctrl:ChildWindow.Resources>
<crtl:ChildWindow.Style>
<StaticResource ResourceKey="Title" />
</crtl:ChildWindow.Style>
</ctrl:ChildWindow&gt

How to add more than one resource to a XAML window?

I have a little problem right now and I don't know how to fix it. I want to add two resources to a window. One is a XAML File style resource, the other a ValueConverter Class.
Both of them work if I use only one resource at a time:
<Window.Resources>
<ResourceDictionary Source="Resources\MyStyles.xaml" />
<Window.Resources>
or
<Window.Resources>
<local:MarginConverter x:Key="adjustMargin"/>
</Window.Resources>
But if I try something like this:
<Window.Resources>
<local:MarginConverter x:Key="adjustMargin"/>
<ResourceDictionary Source="Resources\MyStyles.xaml" />
</Window.Resources>
I get the message the resource is already been set and can not set twice.
I have no idea how to get this done. Is there something like a resource group?
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources\MyStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
<local:MarginConverter x:Key="adjustMargin"/>
</ResourceDictionary>
</Window.Resources>

Resources