2017-05-14 114 views
0

我正在WPF中創建自定義窗口樣式。到目前爲止,我已經能夠在純XAML中完成幾乎可以工作的內容。WPF中的自定義標題按鈕

<ControlTemplate TargetType="{x:Type Window}"> 
    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" 
     Background="{TemplateBinding Background}"> 
     <DockPanel LastChildFill="True"> 
     <Border Height="40" Padding="5" DockPanel.Dock="Top" BorderBrush="#7FA0A0A0" 
      BorderThickness="0,0,0,1"> 
      <Grid WindowChrome.IsHitTestVisibleInChrome="True"> 
      <StackPanel Orientation="Horizontal" HorizontalAlignment="Left"> 
       <Image Source="Resources/Logo/f-dark.png" /> 
       <TextBlock Margin="0,0,0,0" Text="{TemplateBinding Title}" VerticalAlignment="Center" 
       FontSize="16" Foreground="#AF000000"/> 
      </StackPanel> 
      <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" 
        WindowChrome.IsHitTestVisibleInChrome="True" Background="Transparent"> 
       <Button Width="{x:Static SystemParameters.WindowCaptionButtonWidth}" 
        Command="{x:Static SystemCommands.MinimizeWindowCommand}" 
        Style="{StaticResource LinkButton}" 
        WindowChrome.IsHitTestVisibleInChrome="True" 
        IsEnabled="True"> 
       <TextBlock Style="{StaticResource Icon}" FontSize="18">&#xE15B;</TextBlock> 
       </Button> 
       <Button Width="{x:Static SystemParameters.WindowCaptionButtonWidth}" 
        Command="{x:Static SystemCommands.RestoreWindowCommand}" 
        Style="{StaticResource LinkButton}" 
        WindowChrome.IsHitTestVisibleInChrome="True" 
        IsEnabled="True"> 
       <TextBlock Style="{StaticResource Icon}" FontSize="18">&#xE5D0;</TextBlock> 
       </Button> 
       <Button Width="{x:Static SystemParameters.WindowCaptionButtonWidth}" 
        Command="{x:Static SystemCommands.CloseWindowCommand}" 
        Style="{StaticResource LinkButton}" 
        WindowChrome.IsHitTestVisibleInChrome="True" 
        IsEnabled="True"> 
       <TextBlock Style="{StaticResource Icon}" FontSize="18">&#xE5CD;</TextBlock> 
       </Button> 
      </StackPanel> 
      </Grid> 

     </Border> 
     <Border> 
      <ContentPresenter/> 
     </Border> 

     </DockPanel> 
    </Border> 
</ControlTemplate> 

然而,在該窗口鍍鉻按鈕不突出或改變光標(和按鍵使用相同的風格表現爲意)。點擊它們不起作用。爲什麼是這樣?我如何獲得按鈕是互動的?

當我第一次打開的窗口中,我得到的是這樣的: an example window

我也想能夠修復的黑邊,但我的主要問題是,標題按鈕非交互。

回答

2

我已經解決了這兩個問題。事實證明,SystemCommands沒有實現,而只是RoutedCommand。 (他們爲什麼這樣做呢?)

所以,我做了一個附加屬性,該屬性設置爲CommandBindings,我將此風格,每個窗口。

public class Helper 
{ 
    public static bool GetUseWindowCommandBindings(DependencyObject obj) 
    { 
     return (bool)obj.GetValue(UseWindowCommandBindingsProperty); 
    } 

    public static void SetUseWindowCommandBindings(DependencyObject obj, bool value) 
    { 
     obj.SetValue(UseWindowCommandBindingsProperty, value); 
    } 

    public static readonly DependencyProperty UseWindowCommandBindingsProperty = 
     DependencyProperty.RegisterAttached("UseWindowCommandBindings", typeof(bool), typeof(Helper), new PropertyMetadata(false, UseWindowCommandBindingsChanged)); 

    private static void UseWindowCommandBindingsChanged(DependencyObject d, DependencyPropertyChangedEventArgs dpce) 
    { 
     if (d is Window w && dpce.NewValue is bool b && b) 
     { 
      w.CommandBindings.Add(
       new CommandBinding(
        SystemCommands.MinimizeWindowCommand, 
        (s, e) => SystemCommands.MinimizeWindow(w), 
        (s, e) => e.CanExecute = true 
        )); 
      w.CommandBindings.Add(
       new CommandBinding(
        SystemCommands.RestoreWindowCommand, 
        (s, e) => SystemCommands.RestoreWindow(w), 
        (s, e) => e.CanExecute = true 
        )); 
      w.CommandBindings.Add(
       new CommandBinding(
        SystemCommands.MaximizeWindowCommand, 
        (s, e) => SystemCommands.MaximizeWindow(w), 
        (s, e) => e.CanExecute = true 
        )); 
      w.CommandBindings.Add(
       new CommandBinding(
        SystemCommands.CloseWindowCommand, 
        (s, e) => SystemCommands.CloseWindow(w), 
        (s, e) => e.CanExecute = true 
        )); 
     } 
    } 
} 

我不得不把CommandBindings的附着在屬性更改處理程序,因爲WPF直接調用SetValue()。現在,在我的風格中,我添加了這一行:

<Setter Property="view:Helper.UseWindowCommandBindings" Value="True" /> 

現在按鈕按預期工作。要修復黑色邊框,我將我模板中最外面的Border的寬度設置爲{TemplateBinding Width}。但是,如果我最大化窗口,會產生一個巨大的全黑色邊框的問題: enter image description here