c# - 最大化时从顶部拖动自定义窗口标题栏不起作用

标签 c# wpf window customization titlebar

我有一个自定义标题栏,窗口样式设置为无。单击标题栏时,我会检查它是否是双击(即窗口最大化和恢复),如果不是双击,我会执行 Window.DragMove。这对于捕捉到侧面和顶部非常有用。但是当我试图在窗口最大化时拖动它(这通常会恢复窗口),它什么也没做。这是我的代码:

    static Window Window { get { return Application.Current.MainWindow; } }

    /// <summary>
    /// TitleBar_MouseDown - Drag if single-click, resize if double-click
    /// </summary>
    private static void TitleBar_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if (e.ChangedButton == MouseButton.Left)
        {
            if (e.ClickCount == 2)
            {
                AdjustWindowSize();
            }
            else
            {
                Window.DragMove();//Here is where I do the drag move
            }
        }
    }

    /// <summary>
    /// Adjusts the WindowSize to correct parameters when Maximize button is clicked
    /// </summary>
    internal static void AdjustWindowSize()
    {
        if (Window.WindowState == WindowState.Maximized)
        {
            SystemCommands.RestoreWindow(Window);
        }
        else
        {
            SystemCommands.MaximizeWindow(Window);
        }

    }

    #region Button Events

    /// <summary>
    /// CloseButton_Clicked
    /// </summary>
    public static void Close()
    {
        SystemCommands.CloseWindow(Window);
    }

    /// <summary>
    /// MaximizedButton_Clicked
    /// </summary>
    public static void Maximize()
    {
        AdjustWindowSize();
    }

    /// <summary>
    /// Minimized Button_Clicked
    /// </summary>
    public static void Minimize()
    {
        SystemCommands.MinimizeWindow(Window);
    }

    #endregion

Modern UI 和 MahApps.Metro 以某种方式做到了这一点,我简要地查看了他们的源代码,但找不到他们是如何做到的。

提前致谢。

最佳答案

我能够在纯 xaml 中获得所需的标题栏行为,包括 aero snap

结果你可以看到一个自定义标题栏,它是完全可拖动的,双击最大化和恢复并拖动捕捉和取消捕捉。

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

所以现在您不需要任何代码来手动处理标题栏。

可重复使用的样式

你也可以用自定义样式包裹上面,你可以在多个窗口上应用

<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>

用法

<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>

如何在你的代码中实现

看了你的代码后,我确实设法用很少的改动实现了它

变化是

文件: CustomChrome.cs

第 41 行:更改 CaptionHeight = 36,目前为 0。这应该等于您的标题栏高度

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

第 60 行:删除 ((FrameworkElement)sender).MouseDown += TitleBar_MouseDown; 因为不需要

第 70 行:删除不再使用的事件 TitleBar_MouseDown

文件: CornerButtons.xaml

第 13 行:将 WindowChrome.IsHitTestVisibleInChrome="True" 添加到 StackPanel

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

文件: MainWindow.xaml

第 17 行:将 WindowChrome.IsHitTestVisibleInChrome="True" 添加到 StackPanel

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

就是这样,您的应用程序将有一个正常的标题栏,无需处理自定义逻辑

关于c# - 最大化时从顶部拖动自定义窗口标题栏不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24654845/

相关文章:

WPF:自定义窗口

javascript - window.location强制更新?

wpf - 在 Xaml 中动态调整窗口大小

c# - 拖动时 TreeView 自动滚动

c# - 删除 "if"语句时出现问题

c# - 具有非必需参数的 SQL 存储过程

c# - 如何制作 "greenscreen"网络应用程序?

c# - 在动态填充的 TabControl 中为 TabItems 设置正确的 DataContext

c# - 使用 MVVM 从 WPF 中的 TextBox 进行正确的 DataGrid 搜索

WPF:打印分页数据网格的最佳方法