如何创建无边框的WPF窗口并且窗口显示完整?

时间:2019-08-02 06:08:41

标签: c# wpf

我设置了窗口属性ResizeMode="CanResize", WindowStyle="None", 但是当WindowState="Maximized"时,左侧显示不完整。 很奇怪。

<Window x:Class="WpfApp24.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp24"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        AllowsTransparency="True"
        WindowStartupLocation="CenterScreen" 
        WindowState="Maximized"
        ResizeMode="CanResize"
        WindowStyle="None" 
        >
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="220"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="38"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="25"/>
        </Grid.RowDefinitions>

        <TextBlock Text="navBarGroup1" Grid.Row="1" Grid.Column="0"/>

    </Grid>
</Window>

C# code
        public MainWindow()
        {
            InitializeComponent();

            this.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
            this.MaxWidth = SystemParameters.MaximizedPrimaryScreenWidth;
        }

here display uncomplete

1 个答案:

答案 0 :(得分:0)

当您拥有WindowState="Maximized"WindowStyle="None"时,该窗口在屏幕的各个边上延伸超出屏幕的实际边缘几个像素。当最大化时,Windows会修剪窗口的边缘以遮盖通常是调整大小的边缘。

  • 解决方案1 ​​

由于您希望最大化窗口并删除样式,因此属性ResizeMode="CanResize"是无用的。 如果您这样设置属性,它将解决您的问题:

ResizeMode="NoResize"

xaml的完整代码:

<Window x:Class="WpfApp24.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp24"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        AllowsTransparency="True"
        WindowStartupLocation="CenterScreen" 
        WindowState="Maximized"
        ResizeMode="NoResize"
        WindowStyle="None" 
        >
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="220"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="38"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="25"/>
        </Grid.RowDefinitions>

        <TextBlock Text="navBarGroup1" Grid.Row="1" Grid.Column="0"/>

    </Grid>
</Window>
  • 解决方案2

在网格中留出6个边距,以防止元素掉到边缘:

<Grid Margin="6">

完整代码:

<Window x:Class="WpfApp24.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp24"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        AllowsTransparency="True"
        WindowStartupLocation="CenterScreen" 
        WindowState="Maximized"
        ResizeMode="CanResize"
        WindowStyle="None" 
        >
    <Grid Margin="6">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="220"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="38"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="25"/>
        </Grid.RowDefinitions>

        <TextBlock Text="navBarGroup1" Grid.Row="1" Grid.Column="0"/>

    </Grid>
</Window>