Menu item icon added programmatically is not displayed

匆匆过客 提交于 2019-12-17 10:04:34

问题


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

The checked-32-context.png image is used only here, but not displayed :


回答1:


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"))
};

Note also that this is not a relative URI.



来源:https://stackoverflow.com/questions/41484033/menu-item-icon-added-programmatically-is-not-displayed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!