问题
When creating a WPF window with AllowsTransparency="True" WindowStyle="None"
and maximizing it via this.WindowState = WindowState.Maximized;
the Window gets bigger than my screen.
When setting AllowTransparency="False"
i have a border around my Window, but the window won't get bigger than my screen.
In my Case I have a 1920x1080 screen and the window becomes 1934x1094.
On my 1280x1024 screen the window will become 1294x1038.
The Window will become still as big as this, whether or not AllowTransparency is enabled or not, yet with it disabled it works properly.
Setting AllowTransparency before maximizing doesen't work and throws an InvalidOperationException.
How to get a WPF window without a Windows-style border, but yet to maximize properly?
回答1:
It seems a pretty common issue. It appears you'll have to bind your height and width to the actual height/width of the screen as stated in this StackOverflow post:
Borderless window application takes up more space than my screen resolution.
I hope that solves the issue you're facing.
回答2:
A couple year late to the party but just increase your this.borderthickness
for the SizeChanged event like so:
<Window x:Class="MyApp.MainWindow"
ResizeMode="CanResize"
WindowStyle="SingleBorderWindow"
SizeChanged="Window_SizeChanged">
....
Code
....
</Window>
public void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (this.WindowState == WindowState.Maximized)
{
this.BorderThickness = new System.Windows.Thickness(8);
}
else
{
this.BorderThickness = new System.Windows.Thickness(0);
}
}
回答3:
If you only care about the correct dimensions of your window and have no trouble with the appearance, you can wrap the contents of the window in System.Windows.Controls.Canvas
like this:
<Canvas Name="MyCanvas" Width="auto" Height="auto">
...
</Canvas>
Then you can use MyCanvas.ActualWidth
and MyCanvas.ActualHeight
to get the resolution of the current screen, with DPI settings taken into account and in device independent units.
It doesn't add any margins like the maximized window itself does.
It should work with multiple monitors too.
(Canvas accepts UIElement
s as children, so you should be able to use it with any content.)
回答4:
set this property of you window.
MinHeight="100"
MinWidth="100"
Height="auto"
Width="auto"
Hope this will work
来源:https://stackoverflow.com/questions/29391063/wpf-maximized-window-bigger-than-screen