C# | WPF - Custom Title Bar - Drag window from top when maximized doesnt work

99封情书 提交于 2019-11-28 21:44:13

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!