since I am loading dynamic content into a StackPanel I would like to add a ContextMenu on each Object.
Right now I am doing it with System.Windows.Controls.ContextMenu and it's working.
Unfortunately the Menu looks like in an old Window form and not like the nice Menu of MahApps.
On the Internet page can I find the documentation how to add a menue in xaml, but not how to create a Menu via code.
Do you have any ideas?
Thanks for your help :)
Looking up in MahApps.Metro I found that ContextMenu's Style has the Key MetroContextMenu.
So you just can create your normal ContextMenu in CodeBehind. The you could add the following Style to your App.xaml
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.ContextMenu.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
<!-- Accent and AppTheme setting -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Green.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
<!-- Apply MetroStyle for ContextMenu to ContextMenus in Application scope -->
<Style TargetType="ContextMenu" BasedOn="{StaticResource MetroContextMenu}" />
</Application.Resources>
It is only the you have to apply to your ContextMenu. There is no special ContextMenu control provided by MahApps (it is only the Style!).
Related
How is it possible to add a custom font to my WindowsTemplate Studio (WPF) project?
The font is placed in a own folder and that's my App.xaml code:
<Application
x:Class="Creator.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="OnStartup"
Exit="OnExit"
DispatcherUnhandledException="OnDispatcherUnhandledException">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Styles/_FontSizes.xaml" />
<ResourceDictionary Source="/Styles/_Thickness.xaml" />
<ResourceDictionary Source="/Styles/MetroWindow.xaml" />
<ResourceDictionary Source="/Styles/TextBlock.xaml" />
<!--
MahApps.Metro resource dictionaries.
Learn more about using MahApps.Metro at https://mahapps.com/
-->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<!-- Accent and AppTheme setting -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Themes/Light.Blue.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Thanks!
I have already found out how it works for me:
Add your font to a "Fonts" folder
yourFont.ttf (right click) -> Properties:
Build Action -> Resource
Copy to Output Directory -> Do not copy
Use font in code:
xaml -> FontFamily="pack://application:,,,/Fonts/#password"
c# code behind -> TextBox.FontFamily = new FontFamily(new Uri("pack://application:,,,/"), "./Fonts/#password");
I have defined in my WPF application all the styles that I want to apply. The problem is that if I open an other window it doesn't have the same style of the main window because it is defined in an other project.
How can I apply the styles of the main application?
Thank you!
EDIT
This is a part of my App.xaml
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Assets/Styles/Themes/Dark.xaml" />
<ResourceDictionary Source="Assets/Styles/Border.xaml" />
<ResourceDictionary Source="Assets/Styles/BaseBlock.xaml" />
<ResourceDictionary Source="Assets/Styles/Buttons.xaml" />
.....
What I need is to apply the styles defined in the Main Project to the ones defined in Project1 in order to have uniformity.
Put your styles to the ResourceDictionary of your application resources in App.xaml. So you can use your resources everywhere via {StaticResource keyOfYourStyle}. If you want, that your styles be applied to all controls of the type, then use default styles:
<Style TargetType="Border" BasedOn="{StaticResource keyOfYourStyle}>
<Application.Resources>
<ResourceDictionary>
<ThisResourceWillBeAccessedEveryWhere x:Key="ResName"/>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/YourAssembley;component/Resources/ButtonStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
I have following app.xaml:
<Application>
...
<Application.Resources>
<!-- Application-specific resources -->
<ResourceDictionary>
<viewModels:ViewModelLocator x:Key="ViewModelLocator"></viewModels:ViewModelLocator>
<ResourceDictionary.MergedDictionaries>
<!--
Styles that define common aspects of the platform look and feel
Required by Visual Studio project and item templates
-->
<ResourceDictionary Source="/Styles/Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style x:Key="PivotStylePassStore" TargetType="Pivot">
...
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>
It builds fine, but cannot it deploy - there is error:
xaml Duplication assignment to the 'Items' property of the
'ResourceDictionary' object
When style entry is removed - there is no problem. What is going on?
Place all your custom/local styles/resources below the MergedDictionaries.
<Application>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!--
Styles that define common aspects of the platform look and feel
Required by Visual Studio project and item templates
-->
<ResourceDictionary Source="/Styles/Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!-- Application-specific resources -->
<viewModels:ViewModelLocator x:Key="ViewModelLocator"></viewModels:ViewModelLocator>
<Style x:Key="PivotStylePassStore" TargetType="Pivot">
...
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>
Actually it shouldn't make a difference if they are all above or below, but they have to be together. Application.Resources is a property of Application, everything else is basically the "default" property of the Application XAML Element (kind of like Text for TextBoxes or Content for various others). You can't mix them together.
I have the following App.xaml:
<Application
x:Class="Genisyss.V2.Client.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Genisyss.V2.Client"
ShutdownMode="OnExplicitShutdown">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="Resources/ShellResources.xaml" />
<ResourceDictionary>
<local:AppBootstrapper
x:Key="bootstrapper" />
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
ShellResources.xaml looks like this:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
x:Class="Genisyss.V2.Client.Resources.ShellResources"
x:ClassModifier="public">
<ResourceDictionary.MergedDictionaries>
<!-- EXTERNAL RESOURCES -->
<ResourceDictionary
Source="/Teton.Wpf;component/Themes/Generic.xaml" />
<ResourceDictionary
Source="Images.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- controls and templates defined, etc -->
</ResourceDictionary>
Configured this way, the program fails at runtime with "resource not found" or "xaml parse exception".
If I change App.xaml to ALSO include external resources, like this:
<Application
x:Class="Genisyss.V2.Client.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Genisyss.V2.Client"
ShutdownMode="OnExplicitShutdown">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- NOTE ADDITION OF EXTERNAL RESOURCES -->
<ResourceDictionary
Source="/Teton.Wpf;component/Themes/Generic.xaml" />
<ResourceDictionary
Source="Resources/ShellResources.xaml" />
<ResourceDictionary>
<local:AppBootstrapper
x:Key="bootstrapper" />
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Now my program finds the resources at runtime and runs without error. What gives with this? I thought the point of merged resources was ACTUALLY TO MERGE THE RESOURCES, so that you didn't need to declare them in two places.
EDIT
Changed the Source property in ShellResources.xaml to be an absolute pack uri:
pack://application:,,,/Teton.Wpf;component/Themes/Generic.xaml
But this made no difference.
This bug showed up because of the order that things need to be defined in a resource file. I have a WPF controls project that defines some custom controls AND overrides the default styles of several built-in controls.
To make a long story short, I had resources that were defined further down in the Generic.xaml file, and they were being accessed further up - that's a no-no. Resources are apparently parsed only once, in a strict top-down fashion. I was under the impression that they were parsed in a two-or-more pass manner just like normal c# source code.
To avoid this happening, here is a suggested way to lay out your Generic.xaml in a WPF Controls project:
<!-- Shared resources like brushes and colors -->
<!-- DEFAULT control styles, e.g. <Style TargetType="{x:Type TextBox}"... -->
<!-- Control styles for custom controls that are defined in your project -->
Thanks to #Aybe for pushing me to dig deeper on this.
I'm newer with WPF projects and I have a question about the component MahApps.Metro.
I created a MainWindow and other windows in my project, my question is: How to apply the same theme in MainWindow to the others windows of the project? And, I must to apply the ResourceDictionary for all windows or exist any way to do this one time only?
Thank you guys!
In your App.xaml, set the ResourceDictionary property like this:
<Application.Resources>
<ResourceDictionary Source="MyResourceDictionary.xaml"/>
</Application.Resources>
Notice that in order for the styles in your resource dictionary to be applied to all controls of that type, the style should NOT have a key. For example, a style defined like this:
<Style TargetType="{x:Type Button}">
<Setter Property="Content" Value="This is the default button text"/>
</Style>
In the ResourceDictionary will cause all the buttons in the project to have a default value when you create them.
Resource's in WPF work based on their available "scope". Check this article for a detailed explanation
Now Application(App.xaml) is in a higher scope to Window. Thus to share these MahApps control resources across your Window's just move the ResourceDictionary's to the Application.Resources scope
something like:
<Application.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>
</Application.Resources>
Now you no longer need to add these Dictionary's in each Window as they are available implicitly