[英]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.