How to make title bar disappear in WPF window?

2019-02-02 03:17发布

问题:

I know this has been asked before but I've tried answers:

  • How to create a WPF Window without a border that can be resized via a grip only?
  • How to remove the title bar from a window but keep the border

and neither work, the title bar text sits there and im unable to move my grid up to the top of the window so that the grid takes up the whole window. I' am stuck on this.

The XAML for the window :

<Window x:Class="PlayWPF.TimerSlideWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="" Height="95" Width="641" WindowStyle="None" 
    ResizeMode="CanResize" AllowsTransparency="False">
   <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
       <Slider Height="42" HorizontalAlignment="Left" Margin="10,14,0,0" 
               Name="sldTime" VerticalAlignment="Top" Width="495" />
       <TextBox FontSize="18" Height="29" HorizontalAlignment="Left" 
                Margin="510,10,0,0" Name="txtTime" Text="00:00:00" 
                TextAlignment="Center" VerticalAlignment="Top" Width="93" />
   </Grid>
</Window>

回答1:

You need to set the WindowStyle property to None, like I outlined in this answer

<Window ...
    WindowStyle="None"
    WindowState="Maximized"
    WindowStartupLocation="CenterScreen">

You can also set AllowsTransparency="True" and Background="Transparent" if you wish to hide the entire window frame and build your own.

Update based on code added to question

The code you just posted works fine for me. There is no title bar, although there is a Resize border because you specified ResizeMode="CanResize"

You do have some whitespace at the top of your window, but that is because you specified a top Margin for your Slider and TextBox (When you specify a Margin with 4 numbers, it goes Left, Top, Right, Bottom so the 2nd number is your Top Margin)



回答2:

<Window x:Class="BorderlessWindow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        WindowStyle="None"
        BorderBrush="Black"
        BorderThickness="5"
        AllowsTransparency="True"
        >
    <Grid>
        <TextBlock Text="Title Less Window" HorizontalAlignment="Center" FontSize="15" Margin="10" />
    </Grid>
</Window>

The above code works fine for your question "How to make title bar dissappear in WPF window?"



回答3:

Try setting TitleBarHeight="0"



回答4:

I think you should play with ShowTitleBar="False" and back anywhere within your application, either in the Xaml file or in the code behind. That should do the trick