WPF:图像按钮小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14583717/
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
WPF: Image Button small
提问by user1434532
I've created an image button via xaml:
我通过 xaml 创建了一个图像按钮:
<Button x:Name="btnAdd5" Grid.Column="12" Grid.Row="6" Visibility="Visible" >
<Image Source="/MyApp;component/Images/Icons/add-icon.png" />
</Button>
At the xaml designer, everything is fine, but during the Debug Mode I do only see a little dot.
在 xaml 设计器中,一切都很好,但在调试模式期间,我只看到了一个小点。
What did I do wrong?
我做错了什么?
回答by HichemSeeSharp
Try to put the image as below :
尝试将图像放置如下:
<Button>
<Button.Template>
<ControlTemplate>
<Image Source="/MyApp;component/Images/Icons/add-icon.png" x:Name="btnAdd4I" Visibility="Visible" Stretch="Fill" />
</ControlTemplate>
</Button.Template>
</Button>
回答by WorkingWeekendsAtMS
If you are using Visual Studio, you must ALSO add the images to your project file or you will get exactly the behavior you describe. For example, I created an Icons Folder inside my project and separately moved all the icons (.png format) into the folder. EPIC fail! The preview worked fine, however when running the executable the icons were not visible. Apparently they are not added as resources in your executable unless you add a specific reference in your VS project file. Again, preview will work... but that turns out to be a monstrous red herring when troubleshooting the issue. Cost me about 4 hours to resolve!
如果您使用的是 Visual Studio,您还必须将图像添加到您的项目文件中,否则您将获得您所描述的行为。例如,我在我的项目中创建了一个 Icons 文件夹,并将所有图标(.png 格式)单独移动到该文件夹中。史诗失败!预览效果很好,但是在运行可执行文件时,图标不可见。显然,除非您在 VS 项目文件中添加特定引用,否则它们不会作为资源添加到您的可执行文件中。同样,预览将起作用……但在解决问题时,结果证明这是一个可怕的红鲱鱼。花了我大约 4 个小时来解决!
For each Icon I wanted to use, I had to click the project folder inside VS, say "add existing item" and then pick the right file. Here's what you should see in your .csproj file:
对于我想使用的每个图标,我必须单击 VS 中的项目文件夹,说“添加现有项目”,然后选择正确的文件。以下是您应该在 .csproj 文件中看到的内容:
<ItemGroup>
<Resource Include="Icons\cut.png" />
<Resource Include="Icons\page_copy.png" />
<Resource Include="Icons\page_paste.png" />
<Resource Include="Icons\text_bold.png" />
<Resource Include="Icons\text_italic.png" />
<Resource Include="Icons\text_underline.png" />
</ItemGroup>

