Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF WindowChrome: Edges of maximized Window are out of the screen

I use WindowChrome to customize a Window. When I maximize the Window, then the edges are out of the screen. I use the following code to fix this:

<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 are not out of the screen.

SystemParameters.WindowResizeBorderThickness contains not the correct value.

like image 823
Jotrius Avatar asked Apr 19 '16 17:04

Jotrius


2 Answers

The WindowChrome will basically overlap by the size of ResizeBorderThickness when maximized.

If you want your Window completely visible while maximized just use the WindowChrome ResizeBorderThickness (5px) as Margin in your Grid style:

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

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

like image 131
Julian Avatar answered Oct 08 '22 05:10

Julian


Example of how to increase border thickness when a window is maximised. Otherwise, due to oddities of WindowChrome, part of the border will disappear.

This example also strips out the standard window header, so you have to add your own minimize/maximize/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>
like image 31
Contango Avatar answered Oct 08 '22 07:10

Contango