在wpf中居中新页面

时间:2012-11-15 04:33:18

标签: c# wpf

我正在尝试使我的程序居中,以便在它更改页面时它将保持居中。现在,它开始居中,但当我改变页面时,它不再居中。

我尝试使用以下代码,但它不适用于页面。谢谢!

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

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

1 个答案:

答案 0 :(得分:0)

我对页面没有太多了解,但Window确实有Property 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>

我希望这会有所帮助。