Dragging custom window title bar from top when maximized does not work Dragging custom window title bar from top when maximized does not work wpf wpf

Dragging custom window title bar from top when maximized does not work


I am able to get the desired behavior of title bar including aero snap in pure xaml

as result you can see a custom title bar, it is completely draggable, double click to maximize and restore and drag to snap and unsnap.

xaml

<Window x:Class="CSharpWPF.MainWindow"             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"            Title="MainWindow" >    <WindowChrome.WindowChrome>        <WindowChrome CaptionHeight="{Binding ActualHeight,ElementName=titlebar}"/>    </WindowChrome.WindowChrome>    <DockPanel LastChildFill="True">        <Border Background="LightBlue" DockPanel.Dock="Top" Height="25" x:Name="titlebar">            <TextBlock Text="{Binding Title, RelativeSource={RelativeSource FindAncestor,AncestorType=Window},FallbackValue=Title}"                        Margin="10,0,0,0"                       VerticalAlignment="Center">                <TextBlock.Effect>                    <DropShadowEffect Color="White" ShadowDepth="3"/>                </TextBlock.Effect>            </TextBlock>        </Border>        <Border BorderBrush="LightGray" BorderThickness="1" Padding="4">            <TextBlock Text="Window content"/>        </Border>    </DockPanel></Window>

result

result

so now you don't need any code behind to handle title bar manually.

Reusable styles

you can also wrap above in a custom style which you can apply on several windows

<Style TargetType="Window" x:Key="CustomTitleBar">    <Setter Property="WindowChrome.WindowChrome">        <Setter.Value>            <WindowChrome CaptionHeight="{x:Static SystemParameters.CaptionHeight}" />        </Setter.Value>    </Setter>    <Setter Property="Template">        <Setter.Value>            <ControlTemplate TargetType="Window">                <DockPanel LastChildFill="True">                    <Border Background="LightBlue" DockPanel.Dock="Top"                             Height="{x:Static SystemParameters.CaptionHeight}" x:Name="titlebar">                        <Grid>                            <TextBlock Text="{TemplateBinding Title}"                                         Margin="10,0,0,0"                                        VerticalAlignment="Center">                                <TextBlock.Effect>                                    <DropShadowEffect Color="White" ShadowDepth="3"/>                                </TextBlock.Effect>                            </TextBlock>                        </Grid>                    </Border>                    <Border Background="{TemplateBinding Background}" BorderBrush="LightGray"                             BorderThickness="1" Padding="4">                        <ContentPresenter/>                    </Border>                </DockPanel>            </ControlTemplate>        </Setter.Value>    </Setter></Style>

usage

<Window x:Class="CSharpWPF.View"                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"                Title="MainWindow"                 Style="{StaticResource CustomTitleBar}" >    <TextBlock Text="Window content"/></Window>

How to implement in your code

After looking at your code, I did manage to implement it with very little changes

Changes are

File: CustomChrome.cs

line 41: change CaptionHeight = 36, currently it is 0. this should be equal to your title bar height

var chrome = new WindowChrome() { GlassFrameThickness = new Thickness(-1), CaptionHeight = 36 };

line 60: remove ((FrameworkElement)sender).MouseDown += TitleBar_MouseDown; as not required

line 70: remove no more used event TitleBar_MouseDown

File: CornerButtons.xaml

line 13: add WindowChrome.IsHitTestVisibleInChrome="True" to StackPanel

    <StackPanel SnapsToDevicePixels="True" Orientation="Horizontal" WindowChrome.IsHitTestVisibleInChrome="True">

File: MainWindow.xaml

line 17: add WindowChrome.IsHitTestVisibleInChrome="True" to StackPanel

<cc:CornerButtons Grid.Column="2">    <StackPanel Orientation="Horizontal"                WindowChrome.IsHitTestVisibleInChrome="True">

this is all, and your app will have a normal title bar without need to handle custom logic