How to add MahApps.Metro Context Menu - wpf

I fount this Context Menu style that i want to add to my application.
I put it In Windows.Resources ad add the style (Style="{StaticResource MetroContextMenu}") into my ContextMenu - nothing happen and my ContextMenu
is still the same so i try to put it inside App.XAML ans still my ContextMenu have the old style.
Am i missing something ?
Update
<Controls:MetroWindow x:Class="MyApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
WindowStartupLocation="CenterScreen"
Height="670"
Width="1024"
GlowBrush="Black"
ShowMaxRestoreButton="False"
ResizeMode="CanResizeWithGrip"
WindowTransitionsEnabled="False"
TitleCaps="False"
TitleForeground="White"
EnableDWMDropShadow="True">
My context menu:
<ListView.ContextMenu>
<ContextMenu Name="lvFileMenu" Style="{StaticResource MetroContextMenu}">
<MenuItem Name="openCaptureMenuItem" Header="Open file" VerticalAlignment="Center" Height="25">
<MenuItem.Icon>
<Image VerticalAlignment="Center"
Source="pack://application:,,,/Resources/open.ico"/>
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</ListView.ContextMenu>

have you looked at quick start for MahApps?
make sure you have Controls:MetroWindow instead of Window tag

Related

WPF XAML defined MenuItem reuse starts working, then disappears

The following simple code attempts to reuse a MenuItem defined in the Window.Resources on two separate Menus.
<Window x:Class="WpfApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:collections="clr-namespace:System.Collections;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<collections:ArrayList x:Key="menuItemValues">
<MenuItem Header="First"/>
<MenuItem Header="Second"/>
<MenuItem Header="Third"/>
</collections:ArrayList>
<MenuItem x:Key="menuItem" x:Shared="False"
ItemsSource="{StaticResource menuItemValues}"
Header="Shared menu item"/>
</Window.Resources>
<StackPanel>
<Menu HorizontalAlignment="Left" VerticalAlignment="Top">
<StaticResource ResourceKey="menuItem"/>
<StaticResource ResourceKey="menuItem"/>
</Menu>
</StackPanel>
</Window>
This starts out great and when you first select the menus, all looks well. The first menu has the desired MenuItems,
So does the second:
But when you navigate back to the first menu, the MenuItems disappear:
Can someone explain why the menu disappears and a way to get this to work?
This was discovered while investigating another SO question that was getting an exception. I tried to use a strategy discussed on another SO question and it seemed to solve the problem until you navigate back to the menu a second time and it disappears.
I have reproduced this issue on 2 separate machines:
Win 10, VS2013 Ult V12.0.40629.00 Update 5, .NET V4.6.0138
Win 7, VS2013 Prem V12.0.31101.00 Update 4, .NET V4.5.51209
This is happening because, while the top-level MenuItem is x:Shared="False", the MenuItem objects in your collection are not. They are declared once each in the ArrayList collection, and then reused in each instance of the menuItem object that's created.
To get the code to work, you'll need to force WPF to create new instances. One option would be to apply the x:Shared="False" to the collection as well. For example:
<Window x:Class="WpfApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:collections="clr-namespace:System.Collections;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<collections:ArrayList x:Key="menuItemValues" x:Shared="False">
<MenuItem Header="First"/>
<MenuItem Header="Second"/>
<MenuItem Header="Third"/>
</collections:ArrayList>
<MenuItem x:Key="menuItem" x:Shared="False"
ItemsSource="{StaticResource menuItemValues}"
Header="Shared menu item"/>
</Window.Resources>
<StackPanel>
<Menu HorizontalAlignment="Left" VerticalAlignment="Top">
<StaticResource ResourceKey="menuItem"/>
<StaticResource ResourceKey="menuItem"/>
</Menu>
</StackPanel>
</Window>
Of course, given that the items are simply given Header values, you could just use the default MenuItem templating behavior, by providing string values instead of MenuItem values. This allows you to reuse the collection itself (which has no underlying inability for reuse):
<Window x:Class="WpfApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
xmlns:collections="clr-namespace:System.Collections;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<collections:ArrayList x:Key="menuItemValues">
<s:String>First</s:String>
<s:String>Second</s:String>
<s:String>Third</s:String>
</collections:ArrayList>
<MenuItem x:Key="menuItem" x:Shared="False"
ItemsSource="{StaticResource menuItemValues}"
Header="Shared menu item"/>
</Window.Resources>
<StackPanel>
<Menu HorizontalAlignment="Left" VerticalAlignment="Top">
<StaticResource ResourceKey="menuItem"/>
<StaticResource ResourceKey="menuItem"/>
</Menu>
</StackPanel>
</Window>

WPF command pattern

I have an application wide flag that is an app setting.
MyApp.Properties.Settings.Default.SoundMuted
The flag is if my apps sound is muted. I have a number of windows where sounds are being played through .Net’s sound player. I was thinking the flag would be wired up to a toolbar button with a command. I was thinking for muting notification to have my classes that play sound wire up to a class (e.g. SoundManager) hanging off the app that implements NotifyPropertyChange. Then if the user clicks the toolbar button, I would set the Muted property in my SoundManager and have all the soundplayer classes get the PropertyChange and mute.
Is there a better pattern for this? Say I could wire all the soundplayers up to the command and that command would fire.
Also is there some slick way to just wire that app setting as a bindable property in xaml?
Although I've always done the same approach as in my other answer in knocking up that example I have realized that it is unnesssasary. You can bind to the Settings object directly, like this:
<Window x:Class="Test_WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyApp"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:Settings x:Key="SettingsRes"/>
</Window.Resources>
<Grid>
<Menu>
<MenuItem Header="Audio">
<MenuItem Header="Mute" IsCheckable="True"
IsChecked="{Binding Path=Default.SoundMuted, Source={StaticResource ResourceKey=SettingsRes}}"/>
</MenuItem>
</Menu>
<TextBlock Text="{Binding Path=Default.SoundMuted, Source={StaticResource ResourceKey=SettingsRes}}" Height="23" HorizontalAlignment="Left" Margin="18,156,0,0" VerticalAlignment="Top" Width="75" />
</Grid>
</Window>
Again, multiple instances of that resource still work well together, e.g. bind the TextBlock to a second resource:
<Window x:Class="Test_WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyApp"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:Settings x:Key="SettingsRes"/>
<local:Settings x:Key="SettingsRes2"/>
</Window.Resources>
<Grid>
<Menu>
<MenuItem Header="Audio">
<MenuItem Header="Mute" IsCheckable="True" IsChecked="{Binding Path=Default.SoundMuted, Source={StaticResource ResourceKey=SettingsRes}}"/>
</MenuItem>
</Menu>
<TextBlock Text="{Binding Path=Default.SoundMuted, Source={StaticResource ResourceKey=SettingsRes2}}" Height="23" HorizontalAlignment="Left" Margin="18,156,0,0" VerticalAlignment="Top" Width="75" />
</Grid>
</Window>
One way is to make a wrapper class to provide bindable access to the static Settings.Default.
I have since realized this is more work than is needed, please see my other answer
namespace MyApp
{
internal sealed class ResourceWrapper
{
public Settings Default
{
get
{
return Settings.Default;
}
}
}
}
Now, we need to add that as a resource somewhere, could be done in App.xaml, here I've done local to the window that is using it, don't forget the namespace:
xmlns:local="clr-namespace:MyApp"
<Window.Resources>
<local:ResourceWrapper x:Key="SettingsWrapper"/>
</Window.Resources>
Now we need to bind to that, this shows it's use in a MenuItem on that same window:
<Menu>
<MenuItem Header="Audio">
<MenuItem Header="Mute" IsCheckable="True"
IsChecked="{Binding Path=Default.SoundMuted, Source={StaticResource ResourceKey=SettingsWrapper}}"/>
</MenuItem>
</Menu>
Like I said, you can add the resource at the app level or you can create multiple of these ResourceWrappers in different windows/controls, they will all point to the same static underneith.
That full xaml for window with a test TextBlock:
<Window x:Class="Test_WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyApp"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:ResourceWrapper x:Key="SettingsWrapper"/>
</Window.Resources>
<Grid>
<Menu>
<MenuItem Header="Audio">
<MenuItem Header="Mute" IsCheckable="True" IsChecked="{Binding Path=Default.SoundMuted, Source={StaticResource ResourceKey=SettingsWrapper}}"/>
</MenuItem>
</Menu>
<TextBlock Text="{Binding Path=Default.SoundMuted, Source={StaticResource ResourceKey=SettingsWrapper}}" Height="23" HorizontalAlignment="Left" Margin="18,156,0,0" VerticalAlignment="Top" Width="75" />
</Grid>
</Window>

wpf how to update Window

I have made my application multilingual. It changes it's language when you click the corresponding menuitem. This works fine but my problem comes when my parent window doesn't change it's language (menu is in parent window). All child windows change their language. Is there any way to force window to load again? I have done my application with resx-files and also checked that Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture are changed when menuitem is clicked.
Edit:
I have made my own class to do the multilingual part and it is an adaptation from this article's approach: WPF Runtime Localization, it works. Here is a part of my XAML code to show the databindings:
<Window x:Class="Multilingual.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:properties="clr-namespace:Multilingual.Properties"
Height="350" Width="300" Name="mainWindow" ResizeMode="CanMinimize" SizeToContent="Manual" Closing="mainWindow_Closing" KeyDown="mainWindow_KeyDown">
and
<Menu Height="22" Name="menu" Width="{Binding ElementName=mainWindow, Path=Width}" HorizontalAlignment="Left" VerticalAlignment="Top" Background="Snow" BorderThickness="2">
<Menu.BitmapEffect>
<DropShadowBitmapEffect />
</Menu.BitmapEffect>
<MenuItem Header="{x:Static properties:Resources.Menu}">
<MenuItem Header="{x:Static properties:Resources.Language}" >
<MenuItem Header="{x:Static properties:Resources.Deutsch}" Name="itemDeutsch" Click="menuItem_Click" />
<MenuItem Header="{x:Static properties:Resources.English}" Name="itemEnglish" Click="menuItem_Click" />
</MenuItem>
<MenuItem Header="{x:Static properties:Resources.Exit}" Click="itemExit_Click"/>
</MenuItem>
</Menu>
So I think it would be enough if I would be able to update the window somehow.
WPF: How to change the CurrentUICulture at runtime
I found a solution. I changed my databindings to look like this:
<MenuItem Header="{Binding Path=Menu, Source={StaticResource Resources}}">
Where "Menu" corresponds the MenuItem's name in resx-file.

WPF Menu: Wrap items

I'm working on a WPF control that gets placed inside a TabControl in another window, and I have a menu that stretches across the top of my custom control with the letters of the alphabet as such (for indexing purposes):
<UserControl x:Class="thispageclass"
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"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d">
<Grid>
<StackPanel Orientation="Vertical">
<Menu Name="mnu">
<MenuItem Header="A" />
<MenuItem Header="B" />
<MenuItem Header="C" />
...
<MenuItem Header="Y" />
<MenuItem Header="Z" />
</Menu>
[other elements]
</StackPanel>
</Grid>
</UserControl>
If all the letters are present, the menu gets wider than the window so you can't see the last few items without making the window wider. It seems like it should be trivial to make the menu wrap around if the window is too small but I can't seem to figure out how.
Just override the ItemsPanel of your Menu to a WrapPanel
Sample
<Menu.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</Menu.ItemsPanel>
I created my own minimal sample which worked, leading me to figuring out it was my custom styles that were messing up my menu.

WPF Focus Navigation Wrapping

Is there a way to force Focus Navigation (as controlled by the Tab key or MoveFocus method) to wrap inside a given container? I have included code which demonstrates this problem below. What is the easiest way to make Tab move focus from TextBox "Charlie" to TextBox "Able" (and visa-versa for Shift+Tab on TextBox "Able") rather than moving it to MenuItem "Alpha"?
<Window x:Class="NavWrapExample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<DockPanel LastChildFill="True">
<Menu DockPanel.Dock="Top">
<MenuItem Header="Alpha" />
<MenuItem Header="Bravo" />
<MenuItem Header="Charlie" />
</Menu>
<StackPanel>
<TextBox Text="Able" />
<TextBox Text="Baker" />
<TextBox Text="Charlie" />
</StackPanel>
</DockPanel>
</Window>
Use the KeyboardNavigation.TabNavigation attached property, like so:
<StackPanel KeyboardNavigation.TabNavigation="Cycle">
<TextBox Text="Able" />
<TextBox Text="Baker" />
<TextBox Text="Charlie" />
</StackPanel>
Found the answer on Mark Smith's blog.
It sounds like what you want is the same behavior as toolbars: you can tab into them, but once an element within the toolbar gets keyboard focus, focus loops inside. If so, use FocusManager as follows:
<StackPanel FocusManager.IsFocusScope="True">
<!-- Controls go here... -->
</StackPanel>

Resources