单击图像时显示菜单但不显示图标。我试过两种方式:
为上下文菜单项设置图标的方法是什么?
的Xaml:
<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:
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;
}
}
答案 0 :(得分:3)
实际上如果你写的话应该有用:
<MenuItem.Icon>
<Image Source="Images/reset.png" ></Image>
</MenuItem.Icon>
只需右键单击项目中图像的属性,将其设置为内容,然后复制为更新。
此致
答案 1 :(得分:0)
这对我有用:
<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>
由于某些原因,使用<MenuItem Icon="...">
无效。
在资源字典中:
<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>
您需要在项目中包含图像,然后在属性中将类型设置为“ Resource”。您还需要在某处包括此资源字典。我不会说谎-在WPF中设置图像确实很痛苦。但是一旦设置好,它就非常可靠。
如果该图像不起作用,请不要直接在ContextMenu上进行故障排除。相反,请在简单的StackPanel或Grid中尝试以下操作:
<Image Source="{StaticResource Images.TextEditIcon}" Height="14" Width="14"/>
一旦显示,则可以将其添加到ContextMenu。
答案 2 :(得分:0)
在后面使用c#代码可以使用Path Markup Syntax
使用Path语法设置图标// paths for each menu item icon ...
string miniPath1 = "M0,0 L8,0 8,1 8,2 0,2 0,1 z";
string miniPath2 = "F1 M 34,17L 43,17L 43,23L 34,23L 34,17 Z M 35,19L 35,22L 42,22L 42,19L 35,19 Z";
MenuItem mi = new MenuItem { Header = "Menu Item Name", Tag = "My Item" };
Brush brush = Brushes.Black;
mi.Items.Add(new MenuItem() { Header = "Item1", Icon = ConvertPathToImage(miniPath1, brush) });
mi.Items.Add(new MenuItem() { Header = "Item2", Icon = ConvertPathToImage(miniPath2, brush) });
ContextMenu cm = new ContextMenu();
cm.Items.Add(mi);
使用简单的转换:
private Image ConvertPathToImage(string PathPath, Brush brush)
{
Geometry gp = Geometry.Parse(PathPath);
GeometryDrawing gd = new GeometryDrawing(brush, new Pen(brush, 1.0), gp);
DrawingImage di = new DrawingImage { Drawing = gd };
return new Image() { Source = di };
}