Why isnt my WPF menuItem Icon showing? - c#

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.

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>

My menu showing up at the top left of the screen

I built a menu bar using the menu control in WPF and it had been working but at some point the menu started showing up on the top left of my first monitor regardless of where in the screen I had the window. Even if I move the main window to the second monitor the menu still shows up in the first monitor.
Here is the code for the menu control:
<Menu>
<MenuItem Header="_File">
<MenuItem Header="_New" Command="New" InputGestureText="Ctrl+N"/>
<MenuItem Header="_Open" Command="Open" InputGestureText="Ctrl+O" />
<MenuItem Header="_Close" Command="Close" InputGestureText="Ctrl+W" />
<Separator/>
<MenuItem Header="_Save" Command="Save" InputGestureText="Ctrl+S" />
<MenuItem Header="Save _As" Command="SaveAs" InputGestureText="Ctrl+Shift+S" />
<Separator/>
<MenuItem Header="E_xit" Command="{StaticResource CommandBinding_Exit}" InputGestureText="Ctrl+Q" />
</MenuItem>
<MenuItem Header="_Edit">
<MenuItem Header="_Add" Command="{StaticResource CommandBinding_Add}" InputGestureText="" />
<MenuItem Header="_Edit" Command="{StaticResource CommandBinding_Edit}" InputGestureText="" />
<MenuItem Header="_Delete" Command="Delete" InputGestureText="" />
<Separator/>
<MenuItem Header="Cut" Command="Cut" InputGestureText="Ctrl+X" />
<MenuItem Header="Copy" Command="Copy" InputGestureText="Ctrl+C" />
<MenuItem Header="Paste" Command="Paste" InputGestureText="Ctrl+V" />
</MenuItem>
<MenuItem Header="_View">
<MenuItem x:Name="miShowStatusBar" Header="Show Status Bar" IsCheckable="True" IsChecked="True" Click="miShowStatusBar_Click"/>
<MenuItem x:Name="miShowFullPath" Header="Show Full Path" IsCheckable="True" IsChecked="True" Click="miShowFullPath_Click"/>
</MenuItem>
<MenuItem Header="_Help">
<MenuItem Header="_About"/>
</MenuItem>
</Menu>
The menu is not referenced any where in the code behind so I can't figure out what might be causing this odd menu placement.
I think the source of the OP's problem is in the search box under the MenuBar. It may come from the Margin or Width settings of the search box.
I faced the same problem and found the sources. Here is the boilerplate code to reproduce the same error. The problem is a combination of 5 parts, as shown in the following XAML. Remove any one of them will solve the OP's issue.
You need to bind the XAML View to a ViewModel with public property Films. If the Films collection has any element in it, it will also cause the issue (Part 5).
<Grid x:Name="MainContent">
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition />
</Grid.RowDefinitions>
<Grid >
<Menu>
<MenuItem Header="Film">
<MenuItem Header="New"/>
</MenuItem>
</Menu>
</Grid>
<Grid Grid.Row="1">
<Grid>
<!--Part 1: Remove HorizontalScrollBarVisibility="Auto"-->
<ScrollViewer HorizontalScrollBarVisibility="Auto">
<!--Part 2: Remove ItemsSource="{Binding Films}"-->
<ItemsControl ItemsSource="{Binding Films}">
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<!--Part 3: Remove the setter tag-->
<Setter Property="Margin" Value="2" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button>
<Button.Template>
<ControlTemplate TargetType="Button">
<!--Part 4: Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}, Path=ActualWidth}-->
<ContentPresenter Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}, Path=ActualWidth}"/>
</ControlTemplate>
</Button.Template>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</Grid>
</Grid>
</Grid>
This has nothing to do with WPF. It's a possible corruption of the Handedness setting or driver issues if you have a touch screen monitor, tablet PC or have a tablet attached with a bad driver (such as a Wacom drawing tablet).
Type this into the Run dialog: shell:::{80F3F1D5-FECA-45F3-BC32-752C152E456E}.
Once Tablet PC Settings come up, go to the Other tab and in the Handedness section, check the Left Handed option.
I bet that this has something to do with the Xaml-Designer of your Visual Studio instance. I encounter similar strange things because of leaving the Xaml-Designer open while debugging.
Try to kill the XDesProc.exe process and check if the problem still occurs.

How to use .ico as image source for Context menu

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!!

Categories

Resources