Ken
Ken

Reputation: 1404

C# wpf get rid of margin on top

I am making a mainWindow, the code is as below.

<Window x:Class="ConfigUI.Views.MainUIView"
        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:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        xmlns:local="clr-namespace:ConfigUI.Views"
        mc:Ignorable="d"
        Title="MainUIView" Height="450" Width="800" 
        FontFamily="Segoe UI" FontSize="14"
        WindowStartupLocation="CenterScreen" WindowStyle="None"
        >
    <DockPanel>
        <DockPanel DockPanel.Dock="Top" Margin="0">
            <Image Source="..\Assets\logo.png"
                   Width="50" DockPanel.Dock="Left"
                   />
            <StackPanel DockPanel.Dock="Right">
                <Button FontFamily="Segoe MDL2 Assets" FontSize="24" 
                        Content="&#xE106;" Width="50" Height="50" />
            </StackPanel>
            <TextBlock Text="My Cloud" FontSize="24" FontWeight="Bold"
                       HorizontalAlignment="Center" VerticalAlignment="Center"
                       />
        </DockPanel>
        <Grid DockPanel.Dock="Bottom"></Grid>
    </DockPanel>
</Window>

But when I run it, it always shows a little gap on the top that I can't get rid of it.

enter image description here

My question is how to remove the gap on the top of the window? Thanks.

Upvotes: 2

Views: 389

Answers (2)

Shubham Sahu
Shubham Sahu

Reputation: 2015

(Solution 1) This way you will loose drop shadow.

You just set AllowsTransparency="True" in your Window Code. This will remove visible border from window.

<Window x:Class="ConfigUI.Views.MainUIView"
    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:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
    xmlns:local="clr-namespace:ConfigUI.Views"
    mc:Ignorable="d"
    Title="MainUIView" Height="450" Width="800" 
    FontFamily="Segoe UI" FontSize="14"
    WindowStartupLocation="CenterScreen" WindowStyle="None"
    AllowsTransparency="True">

</Window>

(Solution 2) Drop shadow will intact.

<WindowChrome.WindowChrome>
    <WindowChrome CaptionHeight="0" ResizeBorderThickness="5" />
</WindowChrome.WindowChrome>

Full Codes

<Window x:Class="SOWPF.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:SOWPF"
    mc:Ignorable="d" WindowStyle="None"
    Title="MainWindow" Height="450" Width="800">

    <WindowChrome.WindowChrome>
        <WindowChrome CaptionHeight="0" ResizeBorderThickness="5" />
    </WindowChrome.WindowChrome>
</Window>

Output

Tip*

Add ResizeMode="CanResize"

Upvotes: 2

ΩmegaMan
ΩmegaMan

Reputation: 31576

Try this:

<DockPanel VerticalAlignment="Top">

or this

<DockPanel Margin="0,-4,0,0">

Upvotes: 1

Related Questions