动态将图像添加到WPF按钮 [英] Dynamically add image to WPF button

查看:236
本文介绍了动态将图像添加到WPF按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的VB.Net应用程序中,我有一个简单的WPF按钮,该按钮使用Children.Add插入到堆栈面板中.关于如何使用Button.NameButton.Content
向此按钮添加内容和名称,这是相当困难的
我发现的所有内容似乎都指向使用样式或模板,但是我需要在旅途中构造和添加此按钮.

任何帮助将不胜感激.

谢谢,
Jason

In my VB.Net application I have a simple WPF button being inserted into to a stack panel using Children.Add. It is fairly strait forward as to how I add content and a name to this button with Button.Name and Button.Content

Everything that I''m finding seems to point to using styles or templates, but I need this button to be constructed and added on the go.

Any help would be greatly appreciated.

Thanks,
Jason

推荐答案

这是我的工作方式:

This is how I did:

Button button2 = new Button();
//Using PackUri syntax where LoadImages is the name of the project and Images is the folder where the Images are stored as resources.
BitmapImage btm = new BitmapImage(new Uri("/LoadImages;component/Images/Bear.bmp", UriKind.Relative));
Image img = new Image();
img.Source = btm;
img.Stretch = Stretch.Fill;
button2.Content = img;
//Set some additional properties 
button2.Height = 35;
button2.Width = 100;
stackpanel1.Children.Add(button2);



希望它也能为您服务.
:thumbsup:



Hope it will work for you too.
:thumbsup:


塔伦(Tarun)在基本概念中所展示的内容.它不能用作功能齐全的按钮.

查看更现实和功能性的实现:
http://social.msdn.microsoft.com /Forums/zh-CN/wpf/thread/91df562f-414c-4326-ac65-42ef301b5f8f/ [ http://msdn.microsoft.com/en-us/library/cc295235.aspx [ ^ ](请参见图像按钮") ,
WPF自定义控件-没有痛苦 [ WPF:IconButton [
What Tarun demonstrated in just the basic idea. It cannot server as a fully-functional button.

Look at more realistic and functional implementations:
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/91df562f-414c-4326-ac65-42ef301b5f8f/[^],
http://msdn.microsoft.com/en-us/library/cc295235.aspx[^] (see "Image button"),
WPF Custom Controls - Without The Pain[^],
WPF: IconButton[^].

—SA


这篇关于动态将图像添加到WPF按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆