C# WPF 在代码中设置 MenuItem.Icon

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 08:24:44  来源:igfitidea点击:

WPF setting a MenuItem.Icon in code

提问by ScottG

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?

我有一个带有 png 的图像文件夹。我想将 MenuItem 的图标设置为该 png。我如何在程序代码中编写它?

采纳答案by Timothy Fries

menutItem.Icon = new System.Windows.Controls.Image 
       { 
           Source = new BitmapImage(new Uri("images/sample.png", UriKind.Relative)) 
       };

回答by Arcturus

<MenuItem>
  <MenuItem.Icon>
    <Image>
      <Image.Source>
        <BitmapImage UriSource="/your_assembly;component/your_path_here/Image.png" />
      </Image.Source>
    </Image>
  </MenuItem.Icon>
</MenuItem>

Just make sure your image in also included in the project file and marked as resource, and you are good to go :)

只需确保您的图像也包含在项目文件中并标记为资源,您就可以开始了:)

回答by awe

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;

回答by IanR

Arcturus's answer is good because it means you have the image file in your project rather than an independent folder.

Arcturus 的回答很好,因为这意味着您的项目中有图像文件而不是独立文件夹。

So, in code that becomes...

所以,在代码中...

menutItem.Icon = new Image
        {
        Source = new BitmapImage(new Uri("pack://application:,,,/your_assembly;component/yourpath/Image.png"))
        }

回答by Mars

You can also use your Visual Studio to insert a icon. This is the easiest way

您还可以使用 Visual Studio 插入图标。这是最简单的方法

  • Right click at you project in the solution explorer
  • chose Properties
  • Make sure you're in the application page.
  • @ recources you see: Icon and Manifest
  • @ Icon: Click browse and pick your icon.
  • 在解决方案资源管理器中右键单击您的项目
  • 选择属性
  • 确保您在应用程序页面中。
  • @你看到的资源:Icon和Manifest
  • @ 图标:单击浏览并选择您的图标。

Problem solved.

问题解决了。

回答by Python Kid

For those of you using vb.net, to do this you need to use this: menuItem.Icon = New Image() With {.Source = New BitmapImage(New Uri("pack://application:,,,/your_assembly;component/yourpath/Image.png"))}

对于那些使用 vb.net 的人,要做到这一点,你需要使用这个: menuItem.Icon = New Image() With {.Source = New BitmapImage(New Uri("pack://application:,,,/your_assembly;component/yourpath/Image.png"))}

回答by Basti Endres

This is a bit shorter :D

这有点短:D

<MenuItem Header="Example">
   <MenuItem.Icon>
      <Image Source="pack://siteoforigin:,,,/Resources/Example.png"/>
   </MenuItem.Icon>
</MenuItem>

回答by Gary95054

This is what worked for me

这对我有用

<MenuItem Header="delete   ctrl-d" Click="cmiDelete_Click">
    <MenuItem.Icon>
        <Image>
            <Image.Source>
                <ImageSource>Resources/Images/delete.png</ImageSource>
            </Image.Source>
        </Image>
    </MenuItem.Icon>
</MenuItem>