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.
Related
I'm trying to make a shared menu between something on my top menu bar of my app, and the right click context menu of something in my interface in WPF. I've googled aroudn but I can't figure out hwo to share ONLY the menuitems list.
Here is a picture of the UI to help describe it:
The way this works is when an item in the list (as shown in the background) is selected, this menu becomes available to use. I would like to make it so that when you right click an item in the list, it also shows the same menu. I would like to avoid duplicating code, so I defined a resource for MenuItem in my window resources:
<MenuItem x:Key="modUtilsMenu">
<MenuItem Header="{Binding SelectedMod.ModName}" IsEnabled="False" FontWeight="Bold" />
<MenuItem Header="{DynamicResource string_Checkforupdates}" Command="{Binding SelectedModCheckForUpdatesCommand}" ToolTip="{DynamicResource string_tooltip_checksForUpdatesToThisMod}" >
<MenuItem.Icon>
<fa:ImageAwesome Style="{StaticResource EnableDisableImageStyle}" Icon="Cloud" Foreground="{DynamicResource {x:Static adonisUi:Brushes.ForegroundBrush}}" Height="16" Width="16"/>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="{DynamicResource string_RestoremodfromME3Tweaks}" Command="{Binding RestoreModFromME3TweaksCommand}" ToolTip="{DynamicResource string_tooltip_forcesUpdateCheck}" >
<MenuItem.Icon>
<fa:ImageAwesome Style="{StaticResource EnableDisableImageStyle}" Icon="CloudDownload" Foreground="{DynamicResource {x:Static adonisUi:Brushes.ForegroundBrush}}" Height="16" Width="16" RenderOptions.BitmapScalingMode="HighQuality"/>
</MenuItem.Icon>
</MenuItem>
...
I then add it to the interface as a sub element of the Mod Utils menuitem:
<MenuItem Header="{DynamicResource string_ModUtils}" Padding="4" IsEnabled="{Binding SelectedMod, Converter={StaticResource NullEnabledConverter}}">
<StaticResource ResourceKey="modUtilsMenu"/>
</MenuItem>
Obviously this doesn't work as it has a second MenuItem defined in the resource.
However, I am not sure how I can store a "list" of menu items to add as children of another object, as the root container element of MenuItem and ContextMenu are not the same. These are all command based menu items. I will have the same issue with a context menu too - how do I only share the contents and not the container? Do I have to do data binding?
I have looked at How do I share a menu definition between a context menu and a regular menu in WPF, but that seems to be just for single menu items. I suppose I could do it for every one of them, but I'm looking to see if there's a way to do this where I only have to update it in one place instead of three to make it work.
Menu and ContextMenu are both of type ItemsControl. You can treat them like this e.g. bind to a collection of item models and specify a DataTemplate.
The following example creates a collection of MenuItem as XAML resource.
To allow multiple instances of the collection it is important to to set the x:Shared attribute to False. Otherwise the menu will be rendered only in one location of the visual tree, no matter the number of references:
<Window>
<Window.Resources>
<x:Array x:Key="SharedMenuItems"
Type="MenuItem"
x:Shared="False">
<MenuItem Header="File">
<MenuItem Header="Save" />
</MenuItem>
<MenuItem Header="Settings" />
</x:Array>
</Window.Resources>
<StackPanel x:Name="RootPanel" viewModels:Item.IsMarkedAsRead="True">
<Menu ItemsSource="{StaticResource SharedMenuItems}" />
<Grid>
<Grid.ContextMenu>
<ContextMenu ItemsSource="{StaticResource SharedMenuItems}" />
</Grid.ContextMenu>
</Grid>
</StackPanel>
</Window>
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.
I have the following piece of code (XAML C#):
<Menu IsMainMenu="True" DockPanel.Dock="Top">
<MenuItem Name="fileMenu" Header="_File" />
<MenuItem Name="editMenu" Header="_Edit" />
<MenuItem Name="setupMenu" Header="_Setup">
<MenuItem Header="_Language">
<MenuItem.Icon>
//I want to insert image here
</MenuItem.Icon>
</MenuItem>
</MenuItem>
<MenuItem Name="helpMenu" Header="_Help" />
</Menu>
And a resource file named images.resx containing an image called lang.png.
How can I insert the image as an icon for the Menu-Item?
Is there a better way?
As Jason said, it's better to add your images as Resources to your project.
Open "Properties" for your project
Select Vertical-tab Resources
Choose Images from the left ComboBox
Choose "Add Resource -> Add Existing File..." from the right ComboBox
Locate the Image you would like to use, e.g "C1.png" (it will automatically be copied to the Resources folder in the root of your project)
Select properties on your newly added Resource Image
In properties, set Build Action to Resource
Open the designer for the .xaml file containing the Menu and add an Image in MenuItem.Icon and then place the cursor on Image.
xaml
<Menu IsMainMenu="True" DockPanel.Dock="Top">
<MenuItem Name="fileMenu" Header="_File" />
<MenuItem Name="editMenu" Header="_Edit" />
<MenuItem Name="setupMenu" Header="_Setup">
<MenuItem Header="_Language">
<MenuItem.Icon>
<Image/>
</MenuItem.Icon>
</MenuItem>
</MenuItem>
<MenuItem Name="helpMenu" Header="_Help" />
</Menu>
From properties you can now select the symbol on the Source Property and all available Image resources will be displayed.
From this dialog you can also choose "Add", locate an image file on the disk and all the above steps will be made for you by Visual Studio.
The resulting uri for the Image.Source in xaml will look something like this (which ofcourse also can be added by hand)
<Menu IsMainMenu="True" DockPanel.Dock="Top">
<MenuItem Name="fileMenu" Header="_File" />
<MenuItem Name="editMenu" Header="_Edit" />
<MenuItem Name="setupMenu" Header="_Setup">
<MenuItem Header="_Language">
<MenuItem.Icon>
<Image Source="/MenuIconImage;component/Resources/C1.png" />
</MenuItem.Icon>
</MenuItem>
</MenuItem>
<MenuItem Name="helpMenu" Header="_Help" />
</Menu>
You could add this, to the Menu.Icon.
<Image>
<Image.Source>
<BitmapImage UriSource="/ASSEMBLYNAME;component/PATH/IMAGE.png" />
</Image.Source>
<Image>
Currently, I have a context menu with submenus containing checkboxes. I have implemented it in a manner similar to below:
<ContextMenu>
<MenuItem Header="Submenu1">
<MenuItem Header="item1.1" IsTabStop="False">
<CheckBox Content="item1.1 checkbox"/>
</MenuItem>
<MenuItem Header="item1.2" IsTabStop="False">
<CheckBox Content="item1.2 checkbox"/>
</MenuItem>
<MenuItem Header="item1.3" IsTabStop="False">
<CheckBox Content="item1.3 checkbox"/>
</MenuItem>
</MenuItem>
<MenuItem Header="Submenu2">
<MenuItem Header="item2.1" IsTabStop="False">
<CheckBox Content="item2.1 checkbox"/>
</MenuItem>
<MenuItem Header="item2.2" IsTabStop="False">
<CheckBox Content="item2.2 checkbox"/>
</MenuItem>
<MenuItem Header="item2.3" IsTabStop="False">
<CheckBox Content="item2.3 checkbox"/>
</MenuItem>
</MenuItem>
</ContextMenu>
I have set the IsTabStop property of each MenuItem to false to fix an earlier bug I found wherein the focus gets stuck on a single menu item (when TAB key is used). However, what happens now is when Submenu1 is open and I try to move focus through the different menu items using the Up or Down arrow key, the submenu closes immediately and highlights the Submenu2 item in the context menu.
I would like to be able to use TAB or arrow keys in moving the focus through the menu items in each submenu. Thank you in advanced for the help.
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>