How can I make a WPF window maximized on the screen with the mouse cursor?

假如想象 提交于 2019-11-29 00:54:09
Artur Michajluk

You can set it in XAML

<Window Height="300" Width="300" WindowState="Maximized">
</Window>

You need to set SizeToContent to Manual. See other answers for details.

I asked the same question on the MSDN WPF Forum and got an answer with this awesome workaround:

void button_Click(object sender, RoutedEventArgs e)
{
    Window window = new Window();
    window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
    window.SourceInitialized += (s, a) => window.WindowState = WindowState.Maximized;
    window.Show();
}

I've also submitted a bug report about this issue to Microsoft that they are currently reviewing.

Make sure that SizeToContent is set to SizeToContent.Manual, otherwise it won't work.

Starting with the window maximize can be achieved by the following addition to the XAML markup.

<Window Height="300" Width="300" 
  WindowState="Maximized"
  SizeToContent="Manual">
</Window>

The property WindowState is subordinate to SizeToContent, which means that you need to set the latter Manual (if you wish the actual maximization). You can also set SizeToContent to Height or Width (if you wish to maximize the window in one dimension, whereas obeying the size computed based on the controls' sizes in the other).

<Window Height="300" Width="300" 
  WindowState="Maximized"
  SizeToContent="Width">
</Window>

The above will make the window span from top to bottom but not necessarily from left to right edge. It's equivalent to pressing the key combination Win+Shift+Up.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!