Replace Window title with Menu for MahApps.Metro borderless Window - c#

I'm developing a border less WPF window application with MahApps.Metro control.
I want to have my menu where normally the window title goes (Title bar's left side). Like below image:
What I have got so far looks like below image:
I have tried setting HorizontalAlignment="Left", but the menu group remains on the right side of the title bar.
Code for this:
<Controls:MetroWindow.WindowCommands>
<Controls:WindowCommands HorizontalAlignment="Left">
<Menu IsMainMenu="True" x:Name="mnuMainMenu" Height="28" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="12" Background="Transparent" Width="Auto" >
<MenuItem Header="_File" x:Name="mnuFile" Visibility="Visible" Background="Transparent">
<MenuItem Header="_Open" x:Name="mnuOpen" Background="Transparent" Command="{Binding MenuOpenCommand}" />
<MenuItem Header="_Exit" x:Name="mnuExit" Click="btnExit_Click" Background="Transparent"/>
</MenuItem>
<MenuItem Header="_Tools">
<MenuItem Header="_Repeat" x:Name="mnuRepete" Background="Transparent" >
<MenuItem Header="Repeat None" Command="{Binding RepeatNoneCommand}" IsCheckable="True"/>
<MenuItem Header="Repeat One" Command="{Binding RepeatOneCommand}" IsCheckable="True"/>
<MenuItem Header="Repeat All" Command="{Binding RepeatAllCommand}" IsCheckable="True"/>
</MenuItem>
</MenuItem>
<MenuItem Header="_Store" x:Name="smOnlineMode" Background="Transparent" Click="smOnlineMode_Click" IsCheckable="True" />
<MenuItem Header="_Play Mode" x:Name="smPlayMode" Background="Transparent" Click="smPlayMode_Click" IsCheckable="True" IsChecked="True"/>
<MenuItem Header="_Play">
<MenuItem Header="_Play" x:Name="mnuPlay" Background="Transparent" Command="{Binding PlayCommand}"/>
<MenuItem Header="P_ause" x:Name="mnuPause" Background="Transparent" Command="{Binding PauseCommand}"/>
<MenuItem Header="_Stop" x:Name="mnuStop" Background="Transparent" Command="{Binding StopCommand}"/>
<Separator/>
<MenuItem Header="_Next" x:Name="mnuNext" Background="Transparent" Command="{Binding NextTrackCommand}"/>
<MenuItem Header="P_revious" x:Name="mnuPrevious" Background="Transparent" Command="{Binding PreviousTrackCommand}" />
<MenuItem Header="_Mute/UnMute" x:Name="smnuMute" Background="Transparent" Command="{Binding MuteSoundCommand}" />
<!--Command="{Binding MuteSoundCommand}"-->
</MenuItem>
<MenuItem Header="_Help">
<MenuItem Header="_Help" x:Name="smnuOnlineHelp" Background="Transparent" Click="smnuHelp_Click" />
<Separator />
<MenuItem Header="_Register Player" x:Name="smnuRegister" Background="Transparent" Click="smnuRegisterPlayer" />
<MenuItem Header="_About Codero Music Player" x:Name="smnuAbout" Background="Transparent" Click="smnuAboutClick" />
</MenuItem>
</Menu>
</Controls:WindowCommands>
</Controls:MetroWindow.WindowCommands>

You can do something like this
Remove the title from the title bar
Add a MetroWindow.LeftWindowCommands tag
Add windows command tag inside LeftWindowCommands
Place a stackpanel or grid and place what ever you want in the title bar
Code:
<controls:MetroWindow.LeftWindowCommands>
<controls:WindowCommands>
<StackPanel Name="menuHolder" Orientation="Horizontal">
<TextBlock Padding="10,5,10,5" Text="My Window"></TextBlock>
<Menu Name="mymenu" Margin="0,5,0,0">
<MenuItem Name="File" Header="File">
<MenuItem Name="Open" Header="Open"/>
<MenuItem Name="Close" Header="Close"/>
</MenuItem>
<MenuItem Name="Edit" Header="Edit">
<MenuItem Name="Copy" Header="Copy"/>
<MenuItem Name="Paste" Header="Paste"/>
</MenuItem>
</Menu>
</StackPanel>
</controls:WindowCommands>

The solution that works for me in MahApps.Metro 1.6.5 is to bind the TitleTemplate dependency property with the MenuBar and Title Textblock in the Main Window as shown below:
MainWindow.xaml:
<Controls:MetroWindow
x:Class="MahAppsMetroDemo.MainWindow"
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:MahAppsMetroDemo"
mc:Ignorable="d"
Title="ILSpy"
Height="450" Width="800"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
Icon="/MahAppsMetroDemo;component/Resources/ILSpy.ico"
>
<Controls:MetroWindow.TitleTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Menu
Grid.Column="0"
Margin="6,0">
<MenuItem Name="File" Header="File">
<MenuItem Name="Open" Header="Open"/>
<MenuItem Name="Close" Header="Close"/>
</MenuItem>
<MenuItem Name="Edit" Header="Edit">
<MenuItem Name="Copy" Header="Copy"/>
<MenuItem Name="Paste" Header="Paste"/>
</MenuItem>
</Menu>
<TextBlock
Grid.Column="1"
Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type Controls:MetroWindow}},Path=Title}"
HorizontalAlignment="Left" VerticalAlignment="Center"
Padding="10,5,10,5"
Margin="6,0"
FontSize="16"
FontWeight="Bold"
/>
</Grid>
</DataTemplate>
</Controls:MetroWindow.TitleTemplate>
<Grid>
</Grid>
</Controls:MetroWindow>
MainWindow.cs
namespace MahAppsMetroDemo
{
using MahApps.Metro.Controls;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : MetroWindow
{
public MainWindow()
{
InitializeComponent();
}
}
}

Create StackPanel, put your menu into StackPanel and set the property HorizontalAlignment=Left or try to use Margin property again

You have to restyle the MetroWindow for your own. The easiest way for your needs would be to create a custom resource dictionary and copy the MetroWindow.xaml into it and change following line to Grid.Column="0":
MetroWindow.xaml
But do not forget to load that modified resource in App.xaml.

Related

WPF Right-Aligning Menu Item Moves Menu to Centre of Window

I'm trying to right-align a single menu item though a method found here, but applying this method (seen in the code below) moves the menu to the center of the window, and I can't find an elegant solution to move it back to the top.
<DockPanel>
<Menu>
<Menu.ItemsPanel>
<ItemsPanelTemplate>
<DockPanel/>
</ItemsPanelTemplate>
</Menu.ItemsPanel>
<MenuItem Header="File">
<MenuItem Header="Home" Click="MenuItem_Home_Click"/>
<MenuItem Header="About"/>
<MenuItem Header="Settings" Click="MenuItem_Settings_Click"/>
<Separator/>
<MenuItem Header="Exit" Click="MenuItem_Exit_Click"/>
</MenuItem>
<MenuItem Header="Notifications" HorizontalAlignment="Right" FlowDirection="RightToLeft"/>
</Menu>
</DockPanel>
Menu location with code
Don't suppose anyone can work out what I've done wrong here?
I've tried various different methods to fix this, only removing the whole Menu.ItemsPanel code works in returning the menu to it's original location, but that also moves the Notifications menu item to the left.
You need to put it in a container, such as a grid, that restricts it's height.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Menu Grid.Row="0">
<Menu.ItemsPanel>
<ItemsPanelTemplate>
<DockPanel HorizontalAlignment="Stretch"/>
</ItemsPanelTemplate>
</Menu.ItemsPanel>
<MenuItem Header="File">
<MenuItem Header="Home" Click="MenuItem_Home_Click"/>
<MenuItem Header="About"/>
<MenuItem Header="Settings" Click="MenuItem_Settings_Click"/>
<Separator/>
<MenuItem Header="Exit" Click="MenuItem_Exit_Click"/>
</MenuItem>
<MenuItem Header="Notifications" HorizontalAlignment="Right" FlowDirection="RightToLeft"/>
</Menu>
</Grid>

How to get bind to base viewmodel from contextmenu inside treeview?

I'm having trouble getting the selection in a contextMenu to bind to my command EditCommand. The buttons in my tree-view bind to it fine, but in the menu it fails. I have read this is most likely due to the contextMenu being in a different UI tree, but solutions using findAncestor and tags have not worked for me. Is there anyway to do bind and still be able to pass the treeViewItem to the method?
My XAML:
<TreeView Background="Transparent"
Margin="10"
Grid.Column="0" Grid.Row="1"
ItemsSource="{Binding Path=TreeViewItems}">
<TreeView.ItemTemplate >
<HierarchicalDataTemplate DataType="{x:Type model:TreeViewSelection}" ItemsSource="{Binding Configs}" >
<DockPanel HorizontalAlignment="Stretch" Background="Transparent"><!--Transparency allows context click on whole row-->
<DockPanel.ContextMenu>
<ContextMenu DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
<MenuItem
Header="Edit"
Command="{Binding ElementName=userControl, Path=DataContext.EditCommand}"<!--Doesn't work-->
CommandParameter="{Binding}">
<MenuItem.Icon>
<Image Source="../Images/edit.png" />
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</DockPanel.ContextMenu>
<TextBlock DockPanel.Dock="Left" Text="{Binding Title}" />
<StackPanel DockPanel.Dock="Right"
Orientation="Horizontal"
HorizontalAlignment="Right">
<Button Height="23" Width="23"
Command="{Binding ElementName=userControl, Path=DataContext.EditCommand}"<!--Works-->
CommandParameter="{Binding}"
Style="{DynamicResource ImageNoTextButton}"
inf:AttachedProperties.Image="../Images/edit.png"
inf:AttachedProperties.ImageMouseOver="../Images/editMouseOver.png" />
</StackPanel>
</DockPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
Try this:
<DockPanel HorizontalAlignment="Stretch" Background="Transparent"
Tag="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}">
<DockPanel.ContextMenu>
<ContextMenu>
<MenuItem
Header="Edit"
Command="{Binding Path=PlacementTarget.Tag.DataContext.EditCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
CommandParameter="{Binding}">
<MenuItem.Icon>
<Image Source="../Images/edit.png" />
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</DockPanel.ContextMenu>
...
</DockPanel>

How to display keyboard shortcut next to menu item in XAML

I'm working with MenuItem in XAML, using InputGestureText I have added Keyboard shortcuts to menu items. What I'm confused about is how to display keyboard shortcut in the actual menu next to each item that has keyboard shortcut.
I don't want to add text to Header but rather display value of InputGestureText therefore it can be changed without having to modify header.
I'm using WPF Localization Extension
Code for Menu
<Menu Grid.Row="0" Height="30" >
<MenuItem Header="{lex:Loc Project75:lang:mnuFileMenu}">
<MenuItem Header="{lex:Loc Project75:lang:mnuFileMenuNew}">
<MenuItem Header="{lex:Loc Project75:lang:mnuFileMenuNew}" Command="{Binding Path=NewCommand}" InputGestureText="Ctrl+N"/>
<MenuItem Header="{lex:Loc Project75:lang:mnuFileMenuNewEmailTemplate}" Command="{Binding Path=NewEmailTemplateCommand}" />
<MenuItem Header="{lex:Loc Project75:lang:mnuFileMenuNewContact}" Command="{Binding Path=NewContactCommand}" />
</MenuItem>
<MenuItem Header="{lex:Loc Project75:lang:mnuFileMenuImport}" Command="{Binding Path=ImportCommand}" InputGestureText="Ctrl+I"/>
<MenuItem Header="{lex:Loc Project75:lang:mnuFileMenuExport}" Command="{Binding Path=ExportCommand}" InputGestureText="Ctrl+E"/>
<Separator/>
<MenuItem Header="{lex:Loc Project75:lang:mnuFileMenuSave}" Command="{Binding Path=SaveDocumentCommand}" InputGestureText="Ctrl+S"/>
<MenuItem Header="{lex:Loc Project75:lang:mnuFileMenuSaveAll}" Command="{Binding Path=SaveAllDocumentsCommand}" InputGestureText="Ctrl+Shift+S" ToolTip="Closes all open Windows"/>
<Separator/>
<MenuItem Header="{lex:Loc Project75:lang:mnuFileMenuClose}" Command="{Binding Path=CloseDocumentCommand}" InputGestureText="Ctrl+F4"/>
<MenuItem Header="{lex:Loc Project75:lang:mnuFileMenuCloseAll}" Command="{Binding Path=CloseAllDocumentsCommand}" InputGestureText="Ctrl+Shift+F4"/>
<Separator/>
<MenuItem Header="{lex:Loc Project75:lang:mnuFileMenuExit}" Command="{Binding Path=ExitCommand}" InputGestureText="Alt+F4"/>
</MenuItem>
<MenuItem Header="{lex:Loc Project75:lang:mnuHelpMenu}">
<MenuItem Header="{lex:Loc Project75:lang:mnuHelpMenuAbout}" Command="{Binding Path=AboutCommand}"/>
</MenuItem>
</Menu>
Try this out:
<Window.Resources>
<ControlTemplate TargetType="MenuItem" x:Key="controlTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Header}"></TextBlock>
<TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=InputGestureText}" Margin="5,0,0,0"></TextBlock>
</StackPanel>
</ControlTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Menu>
<MenuItem Header="Foo">
<MenuItem Header="Bar" Command="Copy" Template="{StaticResource controlTemplate}"></MenuItem>
</MenuItem>
</Menu>
</Grid>
I'm using the copy command as example but you can use any other command. Header can be localized as you are doing.

How to set the orientation of menu items to the right?

How do I make a certain menuitem's horizontal alignment set to right like the following?
Setting HorizontalAlignment="Right" in XAML wasn't the answer. Anyone know how?
Does this layout help you?
<Menu Height="20" >
<Menu.ItemsPanel>
<ItemsPanelTemplate>
<DockPanel HorizontalAlignment="Stretch"/>
</ItemsPanelTemplate>
</Menu.ItemsPanel>
<MenuItem Header="File">
<MenuItem Header="three"/>
</MenuItem>
<MenuItem Header="Edit">
<MenuItem Header="two"/>
</MenuItem>
<MenuItem Header="Tools">
<MenuItem Header="one"/>
</MenuItem>
<MenuItem Header="Help" HorizontalAlignment="Right">
<MenuItem Header="zero"/>
</MenuItem>
</Menu>
Looks as your example.

Create a menu Bar in WPF?

I want to create a menu bar identical to the one in windows forms in my WPF application.
How would I do this?
The menu option in the WPF controls toolbox only gives a blank bar.
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_File">
<MenuItem Header="_Open"/>
<MenuItem Header="_Close"/>
<MenuItem Header="_Save"/>
</MenuItem>
</Menu>
<StackPanel></StackPanel>
</DockPanel>
Yes, a menu gives you the bar but it doesn't give you any items to put in the bar. You need something like (from one of my own projects):
<!-- Menu. -->
<Menu Width="Auto" Height="20" Background="#FFA9D1F4" DockPanel.Dock="Top">
<MenuItem Header="_Emulator">
<MenuItem Header="Load..." Click="MenuItem_Click" />
<MenuItem Header="Load again" Click="menuEmulLoadLast" />
<Separator />
<MenuItem Click="MenuItem_Click">
<MenuItem.Header>
<DockPanel>
<TextBlock>Step</TextBlock>
<TextBlock Width="10"></TextBlock>
<TextBlock HorizontalAlignment="Right">F2</TextBlock>
</DockPanel>
</MenuItem.Header>
</MenuItem>
:
<StackPanel VerticalAlignment="Top">
<Menu Width="Auto" Height="20">
<MenuItem Header="_File">
<MenuItem x:Name="AppExit" Header="E_xit" HorizontalAlignment="Left" Width="140" Click="AppExit_Click"/>
</MenuItem>
<MenuItem Header="_Tools">
<MenuItem x:Name="Options" Header="_Options" HorizontalAlignment="Left" Width="140"/>
</MenuItem>
<MenuItem Header="_Help">
<MenuItem x:Name="About" Header="&About" HorizontalAlignment="Left" Width="140"/>
</MenuItem>
</Menu>
<Label Content="Label"/>
</StackPanel>
<Container>
<Menu>
<MenuItem Header="File">
<MenuItem Header="New">
<MenuItem Header="File1"/>
<MenuItem Header="File2"/>
<MenuItem Header="File3"/>
</MenuItem>
<MenuItem Header="Open"/>
<MenuItem Header="Save"/>
</MenuItem>
</Menu>
</Container>

Categories

Resources