alansiqueira27
alansiqueira27

Reputation: 8534

How to set margin left and right to center a usercontrol?


myUserControl = new myUserControl(this);
myUserControl.Margin = new Thickness(300, 0, -300, 0);
mainDockPanel.Children.Add(myUserControl);
DockPanel.SetDock(myUserControl, Dock.Top);

I would like to have myUserControl at center of the screen, even when the user maximize the window. I guess I could do it if the code above worked u.U Any ideias? Thank you.

Upvotes: 0

Views: 2028

Answers (1)

Falcon
Falcon

Reputation: 3180

Why don't you just use HorizontalAlignment="Center" and VerticalAlignment="Center" with a fixed Width or a fixed Margin?

You can also wrap it in a stretching control like a Grid, something like:

myUserControl = new myUserControl(this);
Grid grid = new Grid();
myUserControl.HorizontalAlignment = HorizontalAlignment.Center;
myUserControl.VerticalAlignment = VerticalAlignment.Center;
grid.Children.Add(myUserControl);
mainDockPanel.Children.Add(grid);
DockPanel.SetDock(grid, Dock.Top);

Upvotes: 3

Related Questions