Link to home
Start Free TrialLog in
Avatar of gusseology
gusseology

asked on

Programatically adding a WPF control

I'm a newbie at using WPF.  I have a project that is a GUI that has a number of tabs displaying information in different ways.  One of my tabs I want to be a sort of "google earth" type view.  Where I can zoom and pan, etc.  

After reading around a bit, I decided to use WPF to accomplish this.  

So, I took my windows Form, which has a tab control with tab pages.  On one of the tab pages, I added a xaml user control via the element host.  So, I guess what I have, as I understand it, is a xaml control, added to a tab page control, via an element host.

Now, my xaml user control, right now, is super simple, just a hello world button.  I know I eventually want to add more buttons and other controls on the fly (programatically).  So I am just trying to do a simple test where I progrmatically add another button to my xaml user control.

I don't know how to do this in code.  Am I adding it to the xaml user control, if so, how?  Am I adding it to the element host?  The tab page??  I'm not really sure how to begin.

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Sawiner
Sawiner

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gusseology
gusseology

ASKER

Sawiner,

thanks for the info!  I realized my mistake later.  When I created the user control xaml, VS2008 didn't name the canvas by default.  So when i tried to access it in the c# project, I couldn't get to it.  Once I named it (Name = "myName") in the XAML tag, I was of course able to get to it and its methods (like, Children.Add(newBtn)).

I also wanted to place the button strategically, so in case anyone is interested, here is a code snipped on how to place the button in a certain location on the canvas...

newBtn.SetValue(System.Windows.Controls.Canvas.LeftProperty, (double)100);
newBtn.SetValue(System.Windows.Controls.Canvas.TopProperty, (double)100);

Thanks!  Btw, I have another WPF question I'll post soon.  So look out for it!  :)


jesse