开发者

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

开发者 https://www.devze.com 2023-02-17 23:58 出处:网络
myUserControl = new myUserControl(this); myUserControl.Margin = new Thickness(300, 0, -300, 0); mainDockPanel.Children.Add(myUserControl);

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.


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消