我有一个自定义的标题栏,并将窗口样式设置为无。在标题栏上点击,我检查它是否是双击(这样做窗口最大化和恢复),如果它不是双击,我做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
现代UI和MahApps.Metro以某种方式做到了这一点,我简单地看了一下他们的源代码,但找不到他们是如何做到这一点的。
提前谢谢。
发布于 2014-07-09 13:23:57
我能够在纯xaml中获得所需的标题栏行为,包括航空快照
因此,你可以看到一个自定义的标题栏,它是完全可拖动的,双击以最大化,并恢复和拖动以捕捉和取消捕捉。
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行:向StackPanel
添加WindowChrome.IsHitTestVisibleInChrome="True"
<StackPanel SnapsToDevicePixels="True" Orientation="Horizontal" WindowChrome.IsHitTestVisibleInChrome="True">
文件: MainWindow.xaml
第17行:向StackPanel
添加WindowChrome.IsHitTestVisibleInChrome="True"
<cc:CornerButtons Grid.Column="2">
<StackPanel Orientation="Horizontal"
WindowChrome.IsHitTestVisibleInChrome="True">
这就是全部,你的应用程序将有一个普通的标题栏,而不需要处理自定义逻辑
https://stackoverflow.com/questions/24654845
复制