WPF WindowChrome:最大化窗口的边缘不在屏幕上

问题描述 投票:13回答:3

我使用WindowChrome来自定义Window。当我最大化窗口时,边缘不在屏幕上。我使用以下代码来解决此问题:

<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>

我的问题:如何获得正确的像素数,以便边缘不在屏幕之外。

SystemParameters.WindowResizeBorderThickness包含的值不正确。

c# wpf xaml .net-4.5 window-chrome
3个回答
5
投票

当最大化时,WindowChrome将基本上与ResizeBorderThickness的大小重叠。

如果你希望你的窗口在最大化时完全可见,只需在网格样式中使用WindowChrome ResizeBorderThickness (5px)作为Margin

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

否则,如果您希望在窗口最大化时不能看到Border的BorderThickness,除了Grid格式的WindowChrome Border's BorderThickness (2px)之外,还应使用Margin作为ResizeBorderThickness (5px)。那么Margin将是7px。


2
投票

窗口最大化时如何增加边框粗细的示例。否则,由于WindowChrome的奇怪之处,部分边框将消失。

此示例还会删除标准窗口标题,因此您必须添加自己的最小化/最大化/关闭按钮。

<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>

0
投票

基于OP的原始示例,他们几乎就在那里......

当窗口最大化时,Windows似乎忽略ResizeBorderThickness值。使用<Setter Property="Margin" Value="7" />似乎可以工作,但是这个值可能需要根据操作系统进行更改(我在Windows 10上对此进行了测试)。

我建议进行一些小的调整(见下面的代码),例如将WindowStyle="None"ResizeMode="CanResize"添加到Window,以及将Style移动到Window.ResourcesApplication.Resources甚至是ResourceDictionary,将样式的TargetType更改为"{x:Type Panel}"并使用密钥name(例如:x:Key="WindowMainPanelStyle"),因为这会阻止样式自动应用于任何子Grid元素,以及允许样式与继承Panel的任何元素一起使用(例如BorderDockPanelGridStackPanel等)。

<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>
© www.soinside.com 2019 - 2024. All rights reserved.