[英]How to adjust the height of a window to the top of the screen?
我在WPF中有一个窗口,我需要状态适合屏幕顶部,宽度仅需要800像素。 我有这个但不工作:
Title="Title" Height="Auto" Width="800" WindowStartupLocation="CenterScreen" ResizeMode="CanResize" Icon="/img/icon.ico" Loaded="Window_Loaded">
我也试过了,但它给出了错误:
Title="Title" Height="*" Width="800" WindowStartupLocation="CenterScreen" ResizeMode="CanResize" Icon="/img/icon.ico" Loaded="Window_Loaded">
有人知道怎么做吗?
从您的Xaml代码中删除WindowStartupLocation="CenterScreen"
并插入Left="0" Top="0"
。 您可以根据需要增加Left
值。
更新
误会是,我没有意识到窗口应该占据整个屏幕的高度(任务栏除外)。
最终的解决方案是:
<Window ...
WindowStartupLocation="CenterScreen"
Height="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Height}"
Width="800" >
您可以将窗口的高度设置为屏幕高度,然后尝试在Window_Loaded事件中设置窗口的位置,如下所示:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var CurrentWindow = (sender as Window);
CurrentWindow.Height = SystemParameters.PrimaryScreenHeight;
CurrentWindow.Top = 0;
CurrentWindow.Left = SystemParameters.PrimaryScreenWidth / 2 - CurrentWindow.Width / 2;
CurrentWindow.MaxHeight = SystemParameters.WorkArea.Bottom;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.