WPF WindowChrome: edges of maximized window exit screen

I use WindowChrome to customize the window. When I maximize the window, the edges are not displayed on the screen. For this, I use the following code:

<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <WindowChrome.WindowChrome> <WindowChrome CaptionHeight="50" CornerRadius="0" GlassFrameThickness="0" NonClientFrameEdges="None" ResizeBorderThickness="5" UseAeroCaptionButtons="False" /> </WindowChrome.WindowChrome> <Grid> <Grid.Style> <Style TargetType="{x:Type Grid}"> <Setter Property="Margin" Value="0" /> <Style.Triggers> <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=WindowState}" Value="Maximized"> <Setter Property="Margin" Value="{x:Static SystemParameters.WindowResizeBorderThickness}" /> </DataTrigger> </Style.Triggers> </Style> </Grid.Style> <Border BorderThickness="2" BorderBrush="Blue" Background="Yellow" /> </Grid> </Window> 

My problem: how can I get the correct number of pixels so that the edges do not go out of the screen.

SystemParameters.WindowResizeBorderThickness contains an invalid value.

+13
source share
3 answers

WindowChrome will basically overlap the size of the ResizeBorderThickness while maximizing.

If you want your window to be fully visible when maximized, just use WindowChrome ResizeBorderThickness (5px) as a grid-style Margin :

 <Setter Property="Margin" Value="5" /> 

Otherwise, if you want BorderThickness not to be visible while the window is maximized, you should use Border BorderThickness (2px) as Margin in addition to WindowChrome Grid-style ResizeBorderThickness (5px) . Then Margin will be 7px .

+5
source

An example of how to increase the thickness of the border when the window is maximized. Otherwise, due to the oddities of WindowChrome, part of the border will disappear.

This example also removes the standard window title, so you must add your own minimize / minimize / close buttons.

 <Window ResizeMode="CanResizeWithGrip" WindowStyle="SingleBorderWindow"> <!-- Remove window header and border. Use with ResizeMode="CanResizeWithGrip" and WindowStyle="SingleBorderWindow". --> <WindowChrome.WindowChrome> <WindowChrome CaptionHeight="1" CornerRadius ="0" ResizeBorderThickness="4" GlassFrameThickness="0"> </WindowChrome> </WindowChrome.WindowChrome> <Border BorderThickness="1"> <Border.Style> <Style TargetType="{x:Type Border}"> <Style.Triggers> <!-- Add to avoid border disappearing when window is maximised --> <DataTrigger Binding="{Binding WindowState, RelativeSource={RelativeSource AncestorType=Window}}" Value="Maximized"> <Setter Property="Margin" Value="10"/> </DataTrigger> <DataTrigger Binding="{Binding WindowState, RelativeSource={RelativeSource AncestorType=Window}}" Value="Normal"> <Setter Property="Margin" Value="0"/> </DataTrigger> </Style.Triggers> </Style> </Border.Style> <Grid> <!-- Window XAML here. --> <Grid> </Border> </Window> 
+2
source

Based on the original OP example, they were almost there ...

When the window is maximized, Windows seems to ignore the value of ResizeBorderThickness. Using <Setter Property="Margin" Value="7"/> seems to work, but this value may need to be changed depending on the operating system (I tested this on Windows 10).

I recommend making a few small changes (see the code below), such as adding WindowStyle="None" and ResizeMode="CanResize" to the Window , as well as moving the Style to Window.Resources , Application.Resources or even to ResourceDictionary by changing the style of TargetType on "{x:Type Panel}" and using the key name (for example: x:Key="WindowMainPanelStyle" ), as this will prevent the style from automatically applying to any children of the Grid , and also allow the style to be used with all elements that inherit from Panel (e.g. Border , DockPanel , Grid , StackPanel , etc.).

 <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" WindowStyle="None" ResizeMode="CanResize"> <WindowChrome.WindowChrome> <WindowChrome CaptionHeight="50" ResizeBorderThickness="5" /> </WindowChrome.WindowChrome> <Window.Resources> <Style TargetType="{x:Type Panel}" x:Key="WindowMainPanelStyle"> <Setter Property="Margin" Value="0" /> <Style.Triggers> <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=WindowState}" Value="Maximized"> <Setter Property="Margin" Value="7" /> </DataTrigger> </Style.Triggers> </Style> </Window.Resources> <Grid Style="{StaticResource WindowMainPanelStyle}"> <Border BorderThickness="2" BorderBrush="Blue" Background="Yellow" /> </Grid> </Window> 
0
source

Source: https://habr.com/ru/post/1247458/


All Articles