不显示以编程方式添加的菜单项图标 [英] Menu item icon added programmatically is not displayed
本文介绍了不显示以编程方式添加的菜单项图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试以编程方式添加选中的图像时出现显示菜单项图标问题:
I have a display menu item icon issue when trying to add a checked image programmatically:
private void ObjectsCanvas_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
ContextMenu cm = new ContextMenu();
MenuItem mDiag = new MenuItem();
mDiag.Icon = new System.Windows.Controls.Image
{
Source = (new BitmapImage(new Uri("assets/checked-32-context.png", UriKind.Relative)))
};
mDiag.Header = Application.Current.Resources["DiagScreenMenuText"].ToString();
cm.Items.Add(mDiag);
cm.PlacementTarget = sender as Button;
cm.IsOpen = true;
}
checked-32-context.png图像仅在此处使用,但不显示:
The checked-32-context.png image is used only here, but not displayed :
推荐答案
In contrast to XAML, it is necessary to specify the full Resource File Pack URI in code behind:
mDiag.Icon = new System.Windows.Controls.Image
{
Source = new BitmapImage(new Uri(
"pack://application:,,,/assets/checked-32-context.png"))
};
还请注意,这不是相对URI.
Note also that this is not a relative URI.
这篇关于不显示以编程方式添加的菜单项图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文