39

How programmatically create an element based on UserControl and dock it to the DockPanel?

iLemming
  • 34,477
  • 60
  • 195
  • 309

4 Answers4

71
var myControl = new MyUserControl();
DockPanel.SetDock(myControl, Dock.Left);
myDockPanel.Children.Add(myControl);

Also see here and here.

Lars Truijens
  • 42,837
  • 6
  • 126
  • 143
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);

Example

H H
  • 263,252
  • 30
  • 330
  • 514
Dani
  • 14,639
  • 11
  • 62
  • 110
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;