WPF setting a MenuItem.Icon in code

前端 未结 8 1068
心在旅途
心在旅途 2020-12-08 07:05

I have an images folder with a png in it. I would like to set a MenuItem\'s icon to that png. How do I write this in procedural code?

相关标签:
8条回答
  • 2020-12-08 07:31
    menutItem.Icon = new System.Windows.Controls.Image 
           { 
               Source = new BitmapImage(new Uri("images/sample.png", UriKind.Relative)) 
           };
    
    0 讨论(0)
  • 2020-12-08 07:40

    This is how I used it (this way it dont need to be built into the assembly):

    MenuItem item = new MenuItem();
    string imagePath = "D:\\Images\\Icon.png");
    Image icon = new Image();
    icon.Source= new BitmapImage(new Uri(imagePath, UriKind.Absolute));
    item.Icon = icon;
    
    0 讨论(0)
提交回复
热议问题