wpf 如何设置图像上下文菜单项的图标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33083218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set the icon for image contextMenu items
提问by Arul karnan
When I click image it displayed the menu but icon's are not displayed. I tried in two ways:
当我单击图像时,它显示了菜单,但未显示图标。我尝试了两种方式:
- One is I resized the icon that's not working
- Second one is I set the path using icon property that's not working.
- 一个是我调整了不起作用的图标的大小
- 第二个是我使用不起作用的图标属性设置路径。
What's the way to set the icon for context menu items??
为上下文菜单项设置图标的方法是什么?
Xaml:
Xml:
<Image Height="20" Width="20" Source="/CitiCall.WinClient;component/Images/user_icon.png" MouseDown="image1_MouseDown" Margin="0,0,4,6" HorizontalAlignment="Right" Name="image1" Stretch="Fill" VerticalAlignment="Top">
<Image.ContextMenu>
<ContextMenu>
<MenuItem Header="Reset password" Icon="/CitiCall.WinClient;component/Images/reset.png"/>
<!--<MenuItem.Icon>
<Image Source="/CitiCall.WinClient;component/Images/reset.png" ></Image>
</MenuItem.Icon>
</MenuItem>-->
<MenuItem Header="Edit Profile"/>
<MenuItem Header="Settings"/>
<MenuItem Header="About us"/>
</ContextMenu>
</Image.ContextMenu>
</Image>
Xamal.cs:
Xamal.cs:
private void image1_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{
Image image = sender as Image;
ContextMenu contextMenu = image.ContextMenu;
contextMenu.PlacementTarget = image;
contextMenu.IsOpen = true;
}
}
回答by Emmanuel DURIN
Actually it should work if you write :
实际上,如果你写它应该可以工作:
<MenuItem.Icon>
<Image Source="Images/reset.png" ></Image>
</MenuItem.Icon>
Just take care of right clicking to the properties of the images in your project, set it as Content, and Copy if newer.
只需注意右键单击项目中图像的属性,将其设置为内容,如果更新则复制。
Have a look at : WPF image resources
看看:WPF图像资源
Regards
问候
回答by Contango
This worked for me:
这对我有用:
<Button.ContextMenu>
<ContextMenu>
<MenuItem Command="{Binding BringToFront}" ToolTip="Bring to front.">
<MenuItem.Header>
<StackPanel Orientation="Horizontal">
<Image Source="{StaticResource Images.TextEditIcon}" Height="14" Width="14" Margin="-20 0 0 0"/>
<TextBlock>Bring to Front</TextBlock>
</StackPanel>
</MenuItem.Header>
</MenuItem>
</ContextMenu>
</Button.ContextMenu>
For some reason, using <MenuItem Icon="...">did not work.
出于某种原因,使用<MenuItem Icon="...">不起作用。
And in the resource dictionary:
在资源字典中:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:presentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options">
<BitmapImage x:Key="Images.TextEditIcon" UriSource="../Images/TextEditIcon.png" presentationOptions:Freeze="True" />
</ResourceDictionary>
You'll need to include the image in your project, and set the type to "Resource" in properties. You'll also need to include this resource dictionary somewhere. I'm not going to lie - images are a real pain to set up in WPF. But once they are set up, it's very reliable.
您需要在项目中包含图像,并在属性中将类型设置为“资源”。您还需要在某处包含此资源字典。我不会撒谎 - 在 WPF 中设置图像真的很痛苦。但是一旦它们设置好,它就非常可靠。
If this image does not work, do not bother troubleshooting it directly on the ContextMenu. Instead, try something like this in a simple StackPanel or Grid:
如果此图像不起作用,请不要直接在 ContextMenu 上进行故障排除。相反,在一个简单的 StackPanel 或 Grid 中尝试这样的事情:
<Image Source="{StaticResource Images.TextEditIcon}" Height="14" Width="14"/>
Once this is displaying, then you can add it into the ContextMenu.
显示后,您可以将其添加到 ContextMenu 中。

