Make Event handler in menu strip - c#

I'm posting here on SOF because I'm getting frustrated in C# WPF. :(
I don't know how to make an event handler in menustrip of WPF.
I know in ordinary win form and in VB.NET and in Buttons of WPF, JUST BY double clicking the controls (UI) I will be directed instantly in auto generated event handler.
In a menu strip of C#, when I assigned both Name and Click (click="button2Click") and then double click the menu tool or whatever you call that, I am being directed to the event handler of the HEADER of menustriptool.
Under "FILE" is "exit" menu. When I assigned click and name in that "exit" and then double click it, the focus will be in the "FILE".
XAML code:
<MenuItem Header="_FILE" >
<MenuItem Header="_Open | View">
<MenuItem.Icon>
<Image Source="Picture/open.png" />
</MenuItem.Icon>
<MenuItem Header="_Connection String">
</MenuItem>
</MenuItem>
<MenuItem Header="_Exit" Click="exitMenu_click">
<MenuItem.Icon>
<Image Source="Picture/close1.png" />
</MenuItem.Icon>
</MenuItem>
</MenuItem>

Just add command in MenuItem and write handler for it. Simple.
<MenuItem Header="_FILE" >
<MenuItem Header="_Open | View" Command=OpenCommand>
<MenuItem.Icon>
<Image Source="Picture/open.png" />
</MenuItem.Icon>
<MenuItem Header="_Connection String">
</MenuItem>
</MenuItem>

Related

How can add key shortcut on the MenuItem header wpf xaml

I try to search on internet but I not find anything. I want to show on the user when is click on the menu in menuitem what is the shortcut for this command (menuitem).
For example I want to make like "Build Solution" and is can see what shortcut is have.
Here is my xaml what I try:
<MenuItem Name="HelpMenuItem" HorizontalContentAlignment="Center" MouseLeave="HelpButtonMouseLeave" MouseEnter="HelpButtonMouseEnter" Header="{catel:LanguageBinding ResourceName=HelpSubMenu}" Command="{Binding Help}">
<MenuItem.InputBindings>
<KeyBinding Key="F1" Gesture="F1" Modifiers="Control" Command="Help" />
</MenuItem.InputBindings>
</MenuItem>
Is it possible to make it only with xaml
You can use InputGestureText property of the MenuItem. According to MSDN it designed exactly for this purporse (https://msdn.microsoft.com/ru-ru/library/system.windows.controls.menuitem.inputgesturetext%28v=vs.110%29.aspx).
<MenuItem Name="HelpMenuItem" Header="Help" InputGestureText="F1" />
Edit
Unfortunately it's not works for root MenuItems. For this case you can write something like this.
<MenuItem>
<MenuItem.Header>
<DockPanel>
<TextBlock Text="Help" Margin="0,0,10,0" />
<TextBlock Text="Ctrl+F1" />
</DockPanel>
</MenuItem.Header>
</MenuItem>

Window.Icon can not be found [duplicate]

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>

Focus moves through menu items when using TAB key, but not when using arrow keys

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.

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>

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.

Categories