i have a Resource like;
<Window.Resources>
<MenuItem Header="*Rename" x:Key="ctxItem" x:Name="removeItem" Click="removeItem_Click" Padding="5,5,5,5">
<MenuItem.Icon>
<Image Margin="0,0,0,0" Source="../images/removeitem.png" Width="16" Height="16" />
</MenuItem.Icon>
</MenuItem>
In code behind i do the following;
ContextMenu ctxTmp = new ContextMenu();
MenuItem mni = ((MenuItem)this.Resources["ctxItem"]);
MenuItem mniTmp = new MenuItem();
mniTmp.Click += new RoutedEventHandler(removeItem_Click);
mniTmp.Name = "removeItem" + x;
mniTmp.Tag = pic;
mniTmp.Icon = mni.Icon;
mniTmp.Header = mni.Header;
mniTmp.CommandTarget = pic;
ctxTmp.Items.Add(mniTmp);
x++;
return ctxTmp;
And set my object's ContextMenu to the returning item.
The behaviour is like this: It displays my UIelement in the needed canvas, and the contextMenu is fine. But when i add a second object. Context menu still wo开发者_Python百科rks, but the image i'm using is not shown.It's wierd and couldn't figured it out.
Thanks Poyraz
I'm surprised that your Image Source path works.
According to this web site, I will transform your path :
Source="../images/removeitem.png"
into URI
Source="pack://application:,,,/AssemblyName;component/images/removeitem.png"
You cannot reuse the same Image element in two MenuItems. You are effectively putting the same visual in two locations, which is not allowed. You would need to create a new instance of Image, even if it points to the same image.
You can however reuse instances of ImageSource.
精彩评论