[英]How to adjust the height of a window to the top of the screen?
I have a window in WPF and I need to state fit the top of the screen, the width only need 800 pixels. 我在WPF中有一个窗口,我需要状态适合屏幕顶部,宽度仅需要800像素。 I have this but not working:
我有这个但不工作:
Title="Title" Height="Auto" Width="800" WindowStartupLocation="CenterScreen" ResizeMode="CanResize" Icon="/img/icon.ico" Loaded="Window_Loaded">
I also tried this but it gives error: 我也试过了,但它给出了错误:
Title="Title" Height="*" Width="800" WindowStartupLocation="CenterScreen" ResizeMode="CanResize" Icon="/img/icon.ico" Loaded="Window_Loaded">
Does anyone know how to do it? 有人知道怎么做吗?
Remove WindowStartupLocation="CenterScreen"
from your Xaml code and insert Left="0" Top="0"
. 从您的Xaml代码中删除
WindowStartupLocation="CenterScreen"
并插入Left="0" Top="0"
。 You may increase Left
value to your needs. 您可以根据需要增加
Left
值。
UPDATE 更新
Misunderstanding, I didn't get that the window should take the full screen height (except task bar). 误会是,我没有意识到窗口应该占据整个屏幕的高度(任务栏除外)。
The final solution would be: 最终的解决方案是:
<Window ...
WindowStartupLocation="CenterScreen"
Height="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Height}"
Width="800" >
You can set the height of window to screen height and then try to set the location of the window in Window_Loaded event as follows: 您可以将窗口的高度设置为屏幕高度,然后尝试在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.