In WPF, I'm programmatically adding a context menu to a control.
在WPF中,我以编程方式向控件添加上下文菜单。
var contextMenu = new ContextMenu();
contextMenu.Items.Add(new MenuItem { Header = "Copy All", Icon = FindResource("CopyImage") });
contextMenu.Items.Add(new MenuItem { Header = "Copy All with Headers", Icon = FindResource("CopyImage") });
contextMenu.Items.Add(new MenuItem { Header = "Copy Selected", Icon = FindResource("CopyImage") });
contextMenu.Items.Add(new MenuItem { Header = "Copy Selected with Headers", Icon = FindResource("CopyImage") });
CopyImage
is defined in my application resource.
CopyImage是在我的应用程序资源中定义的。
<Image x:Key="CopyImage" Source="../Images/copy.png"/>
At runtime, only the last menu item shows the icon. The other three menu items do not.
在运行时,只有最后一个菜单项显示图标。其他三个菜单项没有。
Does anyone have an explanation for this behavior?
有人能解释这种行为吗?
4 个解决方案
#1
7
Take a look at this article.
看一看这篇文章。
It explains that an Image can only be used in one place at a time. That would explain why it only ended up on the most recent assignment you made in code. Instead, define a BitmapImage and then create a new image using the BitmapImage as the source for each menu item.
它解释说,图像一次只能在一个地方使用。这就解释了为什么它只在最近的代码作业中结束。相反,定义一个位图,然后使用位图作为每个菜单项的源创建一个新图像。
From other article:
从其他文章:
To do this, create a BitmapSource as a resource somewhere:
为此,在某处创建一个位图源作为资源:
<BitmapImage x:Key="MyImageSource" UriSource="../Media/Image.png" />
Then, in your code, use something like:
然后,在代码中,使用如下内容:
<Image Source="{StaticResource MyImageSource}" />
#2
4
Each UI Element can only be placed in one location in the visual tree. You can't use the same Image control on multiple MenuItem's. You need to create separate Image controls for each MenuItem. Otherwise each time you assign it to a new MenuItem, you are just moving it from one to the next.
每个UI元素只能放在可视化树中的一个位置。不能在多个菜单项上使用相同的图像控件。您需要为每个菜单项创建单独的图像控件。否则,每当您将它分配给一个新的MenuItem时,您只是将它从一个对象移动到另一个对象。
<Image x:Key="CopyImage1" Source="../Images/copy.png"/>
<Image x:Key="CopyImage2" Source="../Images/copy.png"/>
<Image x:Key="CopyImage3" Source="../Images/copy.png"/>
<Image x:Key="CopyImage4" Source="../Images/copy.png"/>
var contextMenu = new ContextMenu();
contextMenu.Items.Add(new MenuItem { Header = "Copy All", Icon = FindResource("CopyImage1") });
contextMenu.Items.Add(new MenuItem { Header = "Copy All with Headers", Icon = FindResource("CopyImage2") });
contextMenu.Items.Add(new MenuItem { Header = "Copy Selected", Icon = FindResource("CopyImage3") });
contextMenu.Items.Add(new MenuItem { Header = "Copy Selected with Headers", Icon = FindResource("CopyImage4") });
#3
3
Try this, Icon = new BitmapImage(new Uri("images/copy.png", UriKind.Relative))
试试这个,图标=新的BitmapImage(新Uri(图像/拷贝)。png”,UriKind.Relative))
var contextMenu = new ContextMenu();
contextMenu.Items.Add(new MenuItem { Header = "Copy All", Icon = new BitmapImage(new Uri("images/copy.png", UriKind.Relative)) });
contextMenu.Items.Add(new MenuItem { Header = "Copy All with Headers", Icon = new BitmapImage(new Uri("images/copy.png", UriKind.Relative)) });
contextMenu.Items.Add(new MenuItem { Header = "Copy Selected", Icon = new BitmapImage(new Uri("images/copy.png", UriKind.Relative)) });
contextMenu.Items.Add(new MenuItem { Header = "Copy Selected with Headers", Icon = new BitmapImage(new Uri("images/copy.png", UriKind.Relative)) });
#4
0
All answers were helpful. Here is what I ended up doing based on the pointers from @NathanA:
所有的答案都是有帮助的。以下是我根据@NathanA的指示所做的:
var contextMenu = new ContextMenu();
contextMenu.Items.Add(new MenuItem
{
Header = "Copy All",
Icon = new Image {Source = FindResource("CopyImageSource") as ImageSource}
});
contextMenu.Items.Add(new MenuItem
{
Header = "Copy All with Headers",
Icon = new Image {Source = FindResource("CopyImageSource") as ImageSource}
});
contextMenu.Items.Add(new MenuItem
{
Header = "Copy Selected",
Icon = new Image {Source = FindResource("CopyImageSource") as ImageSource}
});
contextMenu.Items.Add(new MenuItem
{
Header = "Copy Selected with Headers",
Icon = new Image {Source = FindResource("CopyImageSource") as ImageSource}
});
And this in the resource dictionary:
这在资源字典里是这样的:
<BitmapImage x:Key="CopyImageSource" UriSource="../Images/copy.png"/>
#1
7
Take a look at this article.
看一看这篇文章。
It explains that an Image can only be used in one place at a time. That would explain why it only ended up on the most recent assignment you made in code. Instead, define a BitmapImage and then create a new image using the BitmapImage as the source for each menu item.
它解释说,图像一次只能在一个地方使用。这就解释了为什么它只在最近的代码作业中结束。相反,定义一个位图,然后使用位图作为每个菜单项的源创建一个新图像。
From other article:
从其他文章:
To do this, create a BitmapSource as a resource somewhere:
为此,在某处创建一个位图源作为资源:
<BitmapImage x:Key="MyImageSource" UriSource="../Media/Image.png" />
Then, in your code, use something like:
然后,在代码中,使用如下内容:
<Image Source="{StaticResource MyImageSource}" />
#2
4
Each UI Element can only be placed in one location in the visual tree. You can't use the same Image control on multiple MenuItem's. You need to create separate Image controls for each MenuItem. Otherwise each time you assign it to a new MenuItem, you are just moving it from one to the next.
每个UI元素只能放在可视化树中的一个位置。不能在多个菜单项上使用相同的图像控件。您需要为每个菜单项创建单独的图像控件。否则,每当您将它分配给一个新的MenuItem时,您只是将它从一个对象移动到另一个对象。
<Image x:Key="CopyImage1" Source="../Images/copy.png"/>
<Image x:Key="CopyImage2" Source="../Images/copy.png"/>
<Image x:Key="CopyImage3" Source="../Images/copy.png"/>
<Image x:Key="CopyImage4" Source="../Images/copy.png"/>
var contextMenu = new ContextMenu();
contextMenu.Items.Add(new MenuItem { Header = "Copy All", Icon = FindResource("CopyImage1") });
contextMenu.Items.Add(new MenuItem { Header = "Copy All with Headers", Icon = FindResource("CopyImage2") });
contextMenu.Items.Add(new MenuItem { Header = "Copy Selected", Icon = FindResource("CopyImage3") });
contextMenu.Items.Add(new MenuItem { Header = "Copy Selected with Headers", Icon = FindResource("CopyImage4") });
#3
3
Try this, Icon = new BitmapImage(new Uri("images/copy.png", UriKind.Relative))
试试这个,图标=新的BitmapImage(新Uri(图像/拷贝)。png”,UriKind.Relative))
var contextMenu = new ContextMenu();
contextMenu.Items.Add(new MenuItem { Header = "Copy All", Icon = new BitmapImage(new Uri("images/copy.png", UriKind.Relative)) });
contextMenu.Items.Add(new MenuItem { Header = "Copy All with Headers", Icon = new BitmapImage(new Uri("images/copy.png", UriKind.Relative)) });
contextMenu.Items.Add(new MenuItem { Header = "Copy Selected", Icon = new BitmapImage(new Uri("images/copy.png", UriKind.Relative)) });
contextMenu.Items.Add(new MenuItem { Header = "Copy Selected with Headers", Icon = new BitmapImage(new Uri("images/copy.png", UriKind.Relative)) });
#4
0
All answers were helpful. Here is what I ended up doing based on the pointers from @NathanA:
所有的答案都是有帮助的。以下是我根据@NathanA的指示所做的:
var contextMenu = new ContextMenu();
contextMenu.Items.Add(new MenuItem
{
Header = "Copy All",
Icon = new Image {Source = FindResource("CopyImageSource") as ImageSource}
});
contextMenu.Items.Add(new MenuItem
{
Header = "Copy All with Headers",
Icon = new Image {Source = FindResource("CopyImageSource") as ImageSource}
});
contextMenu.Items.Add(new MenuItem
{
Header = "Copy Selected",
Icon = new Image {Source = FindResource("CopyImageSource") as ImageSource}
});
contextMenu.Items.Add(new MenuItem
{
Header = "Copy Selected with Headers",
Icon = new Image {Source = FindResource("CopyImageSource") as ImageSource}
});
And this in the resource dictionary:
这在资源字典里是这样的:
<BitmapImage x:Key="CopyImageSource" UriSource="../Images/copy.png"/>