Window.Icon can not be found [duplicate] - c#

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>

Related

How to Bind IsChecked Property of Menu item

Here My code to bind IsChecked property of menu.
<MenuItem Header="_View">
<MenuItem IsCheckable="True" IsChecked="{Binding ElementName=TermMenu, Path=IsChecked}" Header="Term" />
<MenuItem IsCheckable="True" IsChecked="True" Header="Key" />
<MenuItem IsCheckable="True" IsChecked="True" Header="Hand" />
<MenuItem IsCheckable="True" IsChecked="True" Header="Rule" />
</MenuItem>
Here is the context menu
<ContextMenu x:Key="DataGridColumnHeaderContextMenu" >
<MenuItem x:Name="TermMenu" IsCheckable="True" IsChecked="True" Header="Key Term" />
<MenuItem x:Name="Key" IsCheckable="True" IsChecked="True" Header="Key Term Description" />
<MenuItem x:Name="ShortHand" IsCheckable="True" IsChecked="True" Header="Hand" />
<MenuItem x:Name="Rule" IsCheckable="True" IsChecked="True" Header="Rule" />
</ContextMenu>
Now I want when the context menu item is check the main menu View will be checked automatically. But my code is not working. Please explain me why.
x:Name does not work in resources. the element with the specified name in ElementPath does not exist. this is why the binding doesn't work. you can't access TermMenu from the code behind either. You should bind both to a view-model as #wilford sugested.

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>

Make Event handler in menu strip

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>

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.

Categories