简体   繁体   English

在WPF中将新页面居中

[英]Centering a new page in a wpf

I'm trying to center my program so that when it changes pages that it will remain centered. 我正在尝试使程序居中,以便在更改页面时它将保持居中。 As it stands now, it starts off centered, but when I change the page it is no longer centered. 现在,它开始居中,但是当我更改页面时,它不再居中。

I've tried using the following code, but it doesn't work for a page. 我尝试使用以下代码,但不适用于页面。 Thanks! 谢谢!

this.Left = (SystemParameters.PrimaryScreenWidth / 2) - (this.ActualWidth / 2);

this.Top = (SystemParameters.PrimaryScreenHeight / 2) - (this.ActualHeight / 2);

I don't have much idea about Page but Window do have Property WindowStartupLocation , This Property can have one of the three enum values 1)CenterScreen ,2) Manual , 3) CenterOwner , So if you want your MainWindow to be at Center of Screen then just set the property as 我对Page并不太了解,但是Window确实具有WindowStartupLocation属性,该属性可以具有以下三个枚举值之一:1)CenterScreen,2)Manual,3)CenterOwner,因此,如果您希望MainWindow位于屏幕的中心然后将属性设置为

<myWindow:CustomPage x:Class="WpfApplication4.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:myWindow="clr-namespace:WpfApplication4"
    Title="MainWindow" Height="800" Width="800" WindowStartupLocation="CenterScreen">
<Grid>

</Grid>

I hope this will help. 我希望这将有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM