我有一个自定义标题栏,窗口样式设置为无。在单击标题栏时,我检查它是否是双击(窗口最大化并恢复)如果没有双击,我会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
现代用户界面和MahApps.Metro以某种方式做到这一点,我简要地查看了他们的源代码,但却找不到他们是如何做到的。
提前致谢。
答案 0 :(得分:33)
我能够获得所需的标题栏行为,包括纯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>
结果
所以现在你不需要任何代码来手动处理标题栏。
可重复使用的样式
你也可以在一个自定义样式中包装上面,你可以在几个窗口上应用
<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>
<强> How to implement in your code
强>
在查看您的代码后,我确实设法通过很少的更改来实现它
更改
文件: 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">
这就是全部,您的应用程序将具有正常的标题栏,而无需处理自定义逻辑