Click here to Skip to main content
15,914,500 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i know how to add a control in a normal C# app its

picturebox newpicturbox = new picturebox();
this.controls.add(newpicturbox);


but how do i do it in a WPF?
Posted

The same way, only with the appropriate class/method names.

You have to add controls to a container object (like a grid, border, etc). So, if your form has a grid with the name "LayoutRoot", you'd do this:


this.LayoutRoot.Children.Add(myConrol);


I would create the control and set its properties and *then* add it, but that's just the way I do it.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 31-May-11 15:34pm    
Correct, a 5.
--SA
It will be better if you have a "container" like a Border or simply a Grid to add controls.

However if you want to do in a way similar to Winforms, here is how I tried and it worked:

C#
BitmapImage btm = new BitmapImage(new Uri("Winter.jpg",UriKind.Relative));
            Image img = new Image();
            img.Source = btm;            
           
            this.AddChild(img);

There is no picturebox in WPF.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 31-May-11 15:33pm    
Pretty much same way, a 5.
--SA
Tarun.K.S 31-May-11 15:47pm    
Thanks 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