Menu does not stick on top WPF C# - c#

Just look at images you will understand
When I starts program first view=>Click
When maximize its form its view=>Click
I am very new to WPF and I don't know how to fix this problem this is my code:
<Window x:Class="WpfApplication3.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:WpfApplication3"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Menu Margin="0,0,0,285">
<MenuItem Header="File" Name="meFile"></MenuItem>
<MenuItem Header="Edit" Name="meEdit"></MenuItem>
<MenuItem Header="View" Name="meView"></MenuItem>
<MenuItem Header="Project" Name="meProject"></MenuItem>
<MenuItem Header="Build" Name="meBuild"></MenuItem>
<MenuItem Header="Debug" Name="meDebug"></MenuItem>
<MenuItem Header="Team" Name="meTeam"></MenuItem>
</Menu>
</Grid>
</Window>

Remove Margin altogether and use VerticalAlignment="Top" to make it work with Grid.
Don't use Grid, DockPanel is the way to go. Eg;
<DockPanel LastChildFill="False">
<Menu DockPanel.Dock="Top">
<MenuItem Header="File" Name="meFile"></MenuItem>
<MenuItem Header="Edit" Name="meEdit"></MenuItem>
<MenuItem Header="View" Name="meView"></MenuItem>
<MenuItem Header="Project" Name="meProject"></MenuItem>
<MenuItem Header="Build" Name="meBuild"></MenuItem>
<MenuItem Header="Debug" Name="meDebug"></MenuItem>
<MenuItem Header="Team" Name="meTeam"></MenuItem>
</Menu>
</DockPanel>
You might need to set Height if you won't set LastChildFill = False.

You can try DockPanel as below..
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="File" Name="meFile"></MenuItem>
<MenuItem Header="Edit" Name="meEdit"></MenuItem>
<MenuItem Header="View" Name="meView"></MenuItem>
<MenuItem Header="Project" Name="meProject"></MenuItem>
<MenuItem Header="Build" Name="meBuild"></MenuItem>
<MenuItem Header="Debug" Name="meDebug"></MenuItem>
<MenuItem Header="Team" Name="meTeam"></MenuItem>
</Menu>
<StackPanel></StackPanel>
You can refer here as well.

Related

How can I keep a menu submenu item open?

I'm trying to create a somewhat complex menu item that would allow a user to create a new class. The problem into which I am running is that when I click on a numeric up-down ( from the xceed toolkit ) that the menu item closes, even with the property StaysOpenOnClick set to true.
Users will not like that.
To reproduce, create a WPF project and add the Extended WPF Toolkit through NuGet, then drop the following code into your mainwindow class :
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WhyDoesMyMenuItemCloseWhenClicked"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
mc:Ignorable="d"
x:Class="WhyDoesMyMenuItemCloseWhenClicked.MainWindow"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="21"/>
<RowDefinition />
</Grid.RowDefinitions>
<Menu FontWeight="Bold">
<MenuItem Header="_File">
<MenuItem StaysOpenOnClick="True">
<Grid Height="50" Width="50">
<xctk:IntegerUpDown/>
</Grid>
</MenuItem>
</MenuItem>
</Menu>
</Grid>
</Window>
When I click the text field of the integer up-down, the menu closes.
Why does that keep happening? How can I make it NOT happen?
I have figured out a solution. It is sort of a terribly hacky workaround, but it does the job quite well :
The change is that you create a MenuItem within the MenuItem. Then you define your control within the sub MenuItem's MenuItem.Header property, and set that MenuItem's StaysOpenOnClick property to true.
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WhyDoesMyMenuItemCloseWhenClicked"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
mc:Ignorable="d"
x:Class="WhyDoesMyMenuItemCloseWhenClicked.MainWindow"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="21"/>
<RowDefinition />
</Grid.RowDefinitions>
<Menu FontWeight="Bold">
<MenuItem Header="_File" StaysOpenOnClick="True">
<MenuItem Header="_StaysOpenOnClick">
<MenuItem StaysOpenOnClick="True">
<MenuItem.Header>
<xctk:IntegerUpDown/>
</MenuItem.Header>
</MenuItem>
</MenuItem>
</MenuItem>
</Menu>
</Grid>
</Window>
you can make use of StaysOpenOnClick Property to achieve this

Menu icon appears in design mode but not in debug mode

Menu icon appears in design mode but not in debug mode:
<Menu IsMainMenu="True" DockPanel.Dock="Top">
<MenuItem Header="_Application">
<MenuItem Header="_MasterData" Click="btnMasterData_Click">
<MenuItem.Icon>
<Image Source="database.png" />
</MenuItem.Icon>
</MenuItem>
</MenuItem>
</Menu>

How do I reuse item children via a style in XAML?

I have a WPF submenu that I want to reuse in a few places in my XAML. It's a collection of eight <MenuItem> elements with some complicated bindings that I don't want to copy/paste. However, the holder is different in each case: in one place the parent is a <Menu>, in another place the parent is a <MenuItem> in a <ContextMenu>.
I've been experimenting with <Setter Property="Items"> in my <Style> but I think maybe I'm on the wrong track.
To make it concrete, I'm trying to reduce the code duplication from something like this:
<Menu>
<MenuItem Header="Details" IsCheckable="True" ... />
<MenuItem Header="List" IsCheckable="True" ... />
<MenuItem Header="Thumbnails" IsCheckable="True" ... />
...
</Menu>
...
<ContextMenu>
<MenuItem Header="View">
<MenuItem Header="Details" IsCheckable="True" ... />
<MenuItem Header="List" IsCheckable="True" ... />
<MenuItem Header="Thumbnails" IsCheckable="True" ... />
...
</MenuItem>
</ContextMenu>
How about something like this:
You'll need to create the following collection in your resource dictionary:
<Collections:ArrayList x:Key="MenuItems" x:Shared="false">
<MenuItem Header="Details" />
<MenuItem Header="List" />
<MenuItem Header="Thumbnails" />
</Collections:ArrayList>
You'll need to add the following namespace:
xmlns:Collections="clr-namespace:System.Collections;assembly=mscorlib"
...
And then just use the collection:
<Menu ItemsSource="{StaticResource MenuItems}" />
...
<ContextMenu>
<MenuItem Header="View" ItemsSource="{StaticResource MenuItems}" />
</ContextMenu>

DockPanel doesn't change sizes when i'm cliking MaximizeButton

I create user interface in WPF. I have one problem with menu i used DockPanel for it, all works good but there is one problem when i click MaximizeButton DockPanel size don't change like in this pic.
http://xmages.net/i/3405572
Code:
<Grid >
<DockPanel Background="LightBlue">
<Menu Height="23" Name="menu1" DockPanel.Dock="Top">
<MenuItem Header="File" Name="File_Menu"></MenuItem>
<MenuItem Header="Tasks" Name="Tasks_Menu"></MenuItem>
<MenuItem Header="View" Name="View_Menu"></MenuItem>
<MenuItem Header="HandBook" Name="HandBook_Menu"></MenuItem>
<MenuItem Header="Email" Name="Email_Menu"></MenuItem>
<MenuItem Header="Tools" Name="Tools_Menu"></MenuItem>
<MenuItem Header="Options" Name="Options_Menu"></MenuItem>
<MenuItem Header="Help" Name="Help_Menu"></MenuItem>
</Menu>
.........
I imagine its due to the container of the DockPanel the grid, or the way the Dockpanel is set in this grid.

How to make a control dock in a wpf

I'm trying to make my menu bar fill the screen horizontally like a menu bar should look. When I run the program the window is set to maximize already but the menu bar only fills half the screen on top. I'm not sure how to fix this. So just to be clear I'm trying to make my controls fit to appearance based on the size of the window.
Heres the code:
<Window x:Class="WpfApplication3.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" xmlns:my="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Royale" WindowStyle="ThreeDBorderWindow" SizeToContent="Manual" WindowState="Maximized" xmlns:my1="clr-namespace:System;assembly=mscorlib">
<Window.Resources>
<my1:Double x:Key="Width1">500</my1:Double>
</Window.Resources>
<Grid ShowGridLines="False" Width="Auto">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Menu Height="23" HorizontalAlignment="Left" Name="menu1" VerticalAlignment="Top" Width="503" IsMainMenu="True" Grid.ColumnSpan="2">
<MenuItem Header="File" HorizontalContentAlignment="Stretch">
<MenuItem Header="New">
<MenuItem Header="New Camper" />
</MenuItem>
</MenuItem>
<MenuItem Header="Edit" />
<MenuItem Header="View" />
<MenuItem Header="Add" />
</Menu>
</Grid>
</Window>
Using DockPanel:
http://www.wpftutorial.net/DockPanel.html
<DockPanel LastChildFill="True">
<Button Content="Dock=Top" DockPanel.Dock="Top"/>
<Button Content="Dock=Bottom" DockPanel.Dock="Bottom"/>
<Button Content="Dock=Left"/>
<Button Content="Dock=Right" DockPanel.Dock="Right"/>
<Button Content="LastChildFill=True"/>
</DockPanel>
The simple answer - set your menu to use Grid.ColumnSpan="3", since you have 3 columns.
However, I would recommend nesting your "main" grid inside of a DockPanel or a second Grid (with 2 rows). This way, as you add content (ie: add new columns to your grid), you won't have to constantly adjust your menu to compensate.

Categories