Click here to Skip to main content
15,886,840 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 8-Jun-11 8:34am
v2

This is how I did:

C#
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);


Hope it will work for you too.
:thumbsup:
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 8-Jun-11 14:56pm    
Well, nearly the simplest sample, my 5. It won't look like a button, show text, button states, etc., but it can be another step.
--SA
Tarun.K.S 8-Jun-11 15:06pm    
Thanks for the vote SA but it will look like a button, it will have its click event etc. It will just insert the picture instead of the usual text.
Tarun.K.S 8-Jun-11 15:08pm    
Just to add on, the OP is adding the buttons dynamically, so XAML cannot be used here. But yes a ControlTemplate can be used for the button if the OP wants the button to be stylish, otherwise my solution will work just fine.
Sergey Alexandrovich Kryukov 8-Jun-11 18:15pm    
Yes, I forgot about this requirement when collecting links to my answer -- thanks for reminding me. However, everything XAML can do can be done dynamically (but not visa versa).
--SA
Sergey Alexandrovich Kryukov 8-Jun-11 15:02pm    
For something really functional, please see the links in my solution.
--SA
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
 
Share this answer
 
Comments
Member 12293114 4-Feb-16 1:22am    
Hoe to Add multiple Images to Canvas on button click event in wpf
Sergey Alexandrovich Kryukov 4-Feb-16 1:37am    
Just add some instances of Image elements as children of Canvas using Canvas.Children.Add, Canvas.SetTop and Canvas.SetLeft to position each one.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900