How programmatically create an element based on UserControl and dock it to the DockPanel?
Asked
Active
Viewed 3.4k times
4 Answers
71
var myControl = new MyUserControl();
DockPanel.SetDock(myControl, Dock.Left);
myDockPanel.Children.Add(myControl);

Lars Truijens
- 42,837
- 6
- 126
- 143
-
Any ideas as to why the app would be unresponsive after adding a usercontrol to a dockpanel? – IAbstract May 27 '14 at 17:43
-
1@IAbstract, no that should not happen. – Lars Truijens May 27 '14 at 20:33
-
1I actually figured this out - incorrect URI. :( Seems like this could be more gracefully handled. As soon as you inquire a property of the image, it just freezes. – IAbstract May 27 '14 at 21:19
3
Button TopRect = new Button();
TopRect.Background = new SolidColorBrush(Colors.LightGreen);
TopRect.Height = 50;
TopRect.Content = "Top";
// Dock button to top
DockPanel.SetDock(TopRect, Dock.Top);
// Add docked button to DockPanel
dcPanel.Children.Add(TopRect);
2
var uc = new UserControl1();
uc.SetValue(DockPanel.DockProperty, Dock.Left);
myDockPanel.Children.Add(uc);

Walter Fuchs
- 167
- 2
- 11
0
If your DockPanel's last element is floating in the middle of the panel, check that you've assigned myDockPanel.LastChildFill = false;