如何为图像上下文菜单项设置图标

8

当我点击图片时,菜单显示出来了,但是图标没有显示。我尝试了两种方法:

  1. 一种是重新调整了图标的大小,但这并没有起作用。
  2. 另一种是使用图标属性设置路径,但也不起作用。

有什么方法可以为上下文菜单项设置图标吗?

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;
    }
}
3个回答

12

实际上,如果您写入以下内容,则应该可以正常工作:

<MenuItem.Icon>
  <Image Source="Images/reset.png" ></Image>
</MenuItem.Icon>

只需右键单击项目中图像的属性,将其设置为“内容”,然后选择“仅在较新时复制”。

请参阅:WPF图像资源

此致敬意。


是的,发布的链接非常有用。 - Arul karnan

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>    

您需要在项目中包含图像,并在属性中将类型设置为“资源”。您还需要在某个地方包含此资源字典。我不会撒谎 - 在WPF中设置图像真的很麻烦。但是一旦它们设置好了,就非常可靠。

如果此图像无法正常工作,请不要在ContextMenu上直接进行故障排除。而是尝试在简单的StackPanel或Grid中尝试类似以下的操作:

<Image Source="{StaticResource Images.TextEditIcon}" Height="14" Width="14"/>

一旦这个显示出来了,你就可以把它添加到上下文菜单中。


0

使用C#代码,您可以使用Path Markup Syntax设置图标的路径语法。

// 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 };
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接