How to use .ico as image source for Context menu - c#

This problem is similar to others I have seen here, but none answer the exact question I have.
Code:
<Grid Background="White">
<Grid.ContextMenu>
<ContextMenu>
<MenuItem Header="Test">
<MenuItem.Icon>
<Image Source="../Resources/cut_16.ico"></Image>
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</Grid.ContextMenu>
</Grid>
cut_16.ico is in the correct format and directory and is set to "Resource" for Build Action.
The problem is that at run-time, I get a System.IO.IOException and the icon does not show up.
If I use a .png, it shows up fine, but I have an .ico I need to use.
The text for the menu does show up, however.
I have code to convert a .ico to an image source in code-behind, but I would like to do it in XAML.
Any ideas? Thanks!!

Related

CheckBoxes "stealing" bitmaps from each other [duplicate]

I have a MenuItem like below
<MenuItem Header="Edit">
<MenuItem Header="Copy Direct Link" Icon="{StaticResource CopyIcon}" Command="{Binding CopyImageCommand}" />
<MenuItem Header="Copy Image Data" Icon="{StaticResource CopyIcon}" Command="{Binding CopyImageDataCommand}" />
<MenuItem Header="Paste" Icon="{StaticResource PasteIcon}" Command="{Binding PasteImageCommand}" />
</MenuItem>
Notice the 1st 2 items use the same icon, I get something like below
I tried removing the 2nd item,
<MenuItem Header="Edit">
<MenuItem Header="Copy Direct Link" InputGestureText="Ctrl+C" Icon="{StaticResource CopyIcon}" Command="{Binding CopyImageCommand}" />
<!--<MenuItem Header="Copy Image Data" InputGestureText="Ctrl+Alt+C" Icon="{StaticResource CopyIcon}" Command="{Binding CopyImageDataCommand}" />-->
<MenuItem Header="Paste" InputGestureText="Ctrl+P" Icon="{StaticResource PasteIcon}" Command="{Binding PasteImageCommand}" />
</MenuItem>
then I got something like
How can I reuse Icons?
See this question
An Image can only have one parent so it will be moved from the first MenuItem to the second. You can add the x:Shared attribute like this
<Window.Resources>
<Image x:Key="CopyIcon" x:Shared="False" Source="..." />
</Window.Resources>
From msdn
x:Shared Attribute
When set to false, modifies WPF
resource-retrieval behavior so that
requests for the attributed resource
create a new instance for each request
instead of sharing the same instance
for all requests.
You're most likely declaring CopyIcon as Image type in your resource, something like this:
<Window.Resources>
<Image x:Key="CopyIcon" Source="yourcopyicon.ico"/>
</Window.Resources>
So, the root cause of the problem is, Image is a visual element, since it derives from FrameworkElement (which is a visual element), and a visual element cannot have more than one parent at the same time. That is why the first MenuItem is not showing the icon, since the second MenuItem reset the parent of CopyIcon, making itself parent of the CopyIcon.
Hope this explanation is helpful to you. Now follow what Meleak has said in his response. :-)
Try the following:
<MenuItem Header=“Paste“ >
<MenuItem.Icon><Image Height=“16“ Width=“16“ Source=“paste.jpg“ /></MenuItem.Icon>
</MenuItem>

How can I edit a locked file in Visual Studio?

I have a xaml file that I try to edit, but after I make the change and I run the code I see a little sign of lock in the name of the file. The change is not made,
how can I change it?
<Menu Grid.ColumnSpan="6" IsEnabled="False">
<MenuItem Header="_FILE" FontSize="20" Height="25" Width="310" Padding="30,0,0,0">
<MenuItem Header="_Open_file..." Click="Open_file_Click" FontSize="20"></MenuItem>
<MenuItem Header="_Open_folder.." Click="Open_folder_Click" FontSize="20"></MenuItem>
<MenuItem Header="_open_with_server" Click="open_with_server" FontSize="20"></MenuItem>
<MenuItem Header="_open_with_server" Click="open_with_server" FontSize="20"></MenuItem>
<MenuItem Header="_add_song_to_server" Click="add_song_to_server" FontSize="20"
</MenuItem>
<Separator></Separator>
<MenuItem Header="Exit" Click="CloseApp_click" FontSize="25"></MenuItem>
<ListBox Name="lbFiles" />
</MenuItem>
This is the code I want to edit.
Is the program still running? When I start to debug, I get a lock symbol on the file (very briefly) while the project is building. Depending on your settings/version, the lock may also be applied while the code is running.
i deleted and download visual studio 2019 and its finaly work
this is extremely solution but it work.

WPF Menu Item check mark is blacked in Windows 10

I created a small menu bar with some checked menu items. It is blacked out in Windows 10 but displayed fine in Windows 7
XAML
<Window x:Class="CheckMenuTickinWin10.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:CheckMenuTickinWin10"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<ToolBar>
<Menu>
<MenuItem Header="menu">
<MenuItem Header="Sub" IsCheckable="True" IsChecked="True"/>
<MenuItem Header="Sub" IsCheckable="True" IsChecked="False"/>
<MenuItem Header="Sub" IsCheckable="True" IsChecked="True"/>
</MenuItem>
</Menu>
</ToolBar>
</StackPanel>
</Window>
What should I do in Windows 10 to make the check mark visible?
This background is defined in the default ControlTemplate of the menu item. A template defines the look of a control, if you don't define a template for the control, it will pick up the default template.
The good news is that you are free to edit the ControlTemplate to customize the control as whatever you like. The not so good news is that you have to be comfortable with a bunch of XAML code (hundreds of lines for a single template).
First find the "default" ControlTemplate of the MenuItem in Visual Studio.
In the VS designer, right click the SubMenuItem, and choose "Edit Template" -> "Edit a Copy" from the dropdown menu. This will automatically copy the default template of the SubMenuItem to a style name "MenuItemStyle1" as defined in the window's Resource dictionary.
Remove the black background from the template.
Expand MenuItemStyle1, find the following line that defines the black border.
<Border x:Name="GlyphPanel" BorderBrush="#80DADADA" BorderThickness="1" Background="#FF212121" Height="20" Margin="0,1" Visibility="Hidden" Width="20">
Change the Background color from "#FF212121" to "#FFEEF5FD", and save the change. (#FFEEF5FD is the hightlight color of the border, it is also the color of the light background of the menu, you can find this color in the default template.)
Then apply this new template to all 3 menu items.
<MenuItem Header="menu">
<MenuItem Header="Sub" IsCheckable="True" IsChecked="True" Style="{DynamicResource MenuItemStyle1}" />
<MenuItem Header="Sub" IsCheckable="True" IsChecked="False" Style="{DynamicResource MenuItemStyle1}" />
<MenuItem Header="Sub" IsCheckable="True" IsChecked="True" Style="{DynamicResource MenuItemStyle1}" />
</MenuItem>

how to find out which child of a WPF container the contextmenu was opened for?

Because ToolbarTray does not support proper ItemsSource binding I'm doing things in code behind. Namely filling the tray. How can I tell which toolbar the contextmenu was opened for (with CommandParameter)?
<UserControl.Resources>
<ContextMenu x:Key="ToolbarContextMenu">
<MenuItem Header="Move to top" Command="{ui:CommandHandler MoveToTop}" />
<MenuItem Header="Move to left" Command="{ui:CommandHandler MoveToLeft}" />
<MenuItem Header="Make float" Command="{ui:CommandHandler MakeFloat}" />
</ContextMenu>
</UserControl.Resources>
<ToolBarTray x:Name="MainToolbarTray" Orientation="{Binding Orientation}" ContextMenu="{StaticResource ToolbarContextMenu}"/>
In example above I use a toolkit where commands in ViewModel are decorated with attribute and in xaml command can be defined like this.
(Not the same as suggested. I don't want to find ToolbarTray, but the Toolbar)
EDIT: I just assigned the ContextMenu to Toolbar in code behind and used PlacementTarget as suggested.

Why isnt my WPF menuItem Icon showing?

I can see my Icon in Designer View, but when I run the program it disappears. What am i missing?
EDIT: The Icon itself is 25 by 25 pixels
<DockPanel>
<Menu DockPanel.Dock="Top" HorizontalAlignment="Right" Background="Transparent">
<MenuItem Header="Help">
<MenuItem.Icon>
<Image Source="Resources/Help.png" />
</MenuItem.Icon>
<MenuItem Header="About" />
<MenuItem Header="Update TechTools" />
</MenuItem>
</Menu>
</DockPanel>
You need to set properties on your Help.png file. Righ-click on it, select properties. You want Build action to "Content" and Copy to output directory either Copy Always or Copy if Newer.
You can also consider setting it as Resource, which will embedd it in the program itself.

Categories

Resources