天天看點

WPF--->自定義視窗,标題欄輕按兩下放大與還原&點選拖動的2種實作

文章目錄

    • 1.手動實作
    • 2.繼承微軟的Title功能

1.手動實作

Border為标題欄的邊框,Border的名稱為"header"

header = (Border)Template.FindName("header", this);
            header.MouseMove += (o, e) =>
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    this.DragMove();
                }
            };
            header.MouseLeftButtonDown += (o, e) =>
            {
                if (e.ClickCount >= 2)
                {
                    maxBtn.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                }
            };
           

2.繼承微軟的Title功能

1.使用依賴項屬性對header的高度進行綁定,當點選這一高度會有點選title的功能

public int HeaderHeight
        {
            get { return (int)GetValue(HeaderHeightProperty); }
            set { SetValue(HeaderHeightProperty, value); }
        }

// Using a DependencyProperty as the backing store for HeaderHeight.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty HeaderHeightProperty =DependencyProperty.Register("HeaderHeight", typeof(int), typeof(CommonWindow), new PropertyMetadata(0));

public CustomWindow
{
	BindingOperations.SetBinding(chrome, WindowChrome.CaptionHeightProperty,new Binding(nameof(HeaderHeight)) { Source = this });
}

           
  1. 在xaml中進行依賴屬性的綁定,并且在不需要title功能的地方加上

    WindowChrome.IsHitTestVisibleInChrome="True"

<Border Name="header" Background="{DynamicResource WindowTitleBrush}" BorderThickness="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderThickness}">
	<StackPanel WindowChrome.IsHitTestVisibleInChrome="True" DockPanel.Dock="Right"  HorizontalAlignment="Right" VerticalAlignment="Top" Orientation="Horizontal" Height="Auto" >
             <Button x:Name="btnMin"/>
             <Button x:Name="btnMax"/>
             <Button x:Name="btnClose"/>
    </StackPanel>
</Boder>
           

繼續閱讀