2014-07-09 52 views
3

我有一個自定義標題欄,窗口樣式設置爲無。在標題欄的點擊上,我檢查它是否是雙擊(如果沒有雙擊,那麼窗口最大化和還原),我做Window.DragMove。這對於捕捉側面和頂部非常有用。但是,當我試圖拖動窗口時,它最大化(這通常會恢復窗口),它什麼都不做。這裏是我的代碼:C#| WPF - 自定義標題欄 - 最大化時拖動窗口不起作用

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確實不知何故,我看了看他們的源代碼簡單,但無法找到他們是如何做到這一點。

在此先感謝。

回答

25

我能夠獲得包括純XAML

Aero對齊作爲結果,你可以看到一個自定義標題欄標題欄的期望的行爲,它是完全可拖動,雙擊最大化和恢復並拖動搶購併取消綁定。

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> 

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

這一切,你的應用程序將無需處理自定義邏輯

+0

正常的標題欄我試過了,但沒有奏效。它做了一些時髦的東西。 –

+0

你可以發佈你的應用程序的工作示例?我可以在這裏嘗試一下嗎? – pushpraj

+0

你知道該怎麼做嗎? –

相關問題