Click here to Skip to main content
15,885,918 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I m new to the coding scene and have been getting stuck on geting a WPF UserControl to open in a dock panel on a button click. Please help. I can get it to work in a WinForm but the same code doesn't work for a WPF UserControl to a dock panel.
VB
Dim obj1 As New UserControl1
obj1.AutoSize = True
obj1.AutoScroll = True
frmMain.pnlUser.Controls.Add(obj1)

the code above is what i was using in WinForms to call the WinForms UserControl and it worked just fine.
Posted
Updated 13-Nov-11 11:30am
v2
Comments
Sergey Alexandrovich Kryukov 13-Nov-11 17:31pm    
Fixed number of mistakes, removed irrelevant tag, added formatting.
--SA
Sergey Alexandrovich Kryukov 13-Nov-11 17:32pm    
Did you try to compile the above code in WPF of Forms?
--SA

1 solution

Why would you think that your Forms code could work in WPF? These libraries are very different.

You need to do something like this:
VB
Dim myUserControl As New MyUserControl

'...

myDockPanel.Children.Add(myUserControl)
DockPanel.SetDock(myUserControl, Dock.Bottom)


By the way, avoid those names: obj1, UserControl1. Names should be semantic; no numbers in names; in general, observer good Microsoft naming conventions.

—SA
 
Share this answer
 
Comments
Zachary.shupp 13-Nov-11 19:11pm    
i tried the above with differnt names and got errors

Dim red_left As New red_left

'...

dock_left.Children.Add(red_left)
DockPanel.SetDock(red_left, Dock.Bottom)

im getting 2 errors:

Error 2 Value of type 'WpfApplication1.red_left' cannot be converted to 'System.Windows.UIElement'. C:\Users\Zack\documents\visual studio 2010\Projects\WpfApplication usercontrol test\WpfApplication usercontrol test\MainWindow.xaml.vb 11 27 WpfApplication usercontrol test

any suggestions on how to fix this?
Sergey Alexandrovich Kryukov 8-Mar-12 19:54pm    
I have no idea what is red_left, but apparently you should use a type derived from System.Windows.UIElement.
--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