2017-08-24 77 views
0

我希望把對鉻按鈕,我發現這個解決方案Solution.WPF背景renderin錯誤

但它渲染backgrond爲黑色。我沒有在任何地方設置黑色,它是怎樣的黑色?

<WindowChrome.WindowChrome> 
    <WindowChrome GlassFrameThickness="0.5,28,0,0.5" NonClientFrameEdges="Right"/> 
</WindowChrome.WindowChrome> 
<Window.Template> 
    <ControlTemplate> 
     <ContentPresenter Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window}, Path=Content}"/> 
    </ControlTemplate> 
</Window.Template> 
<Grid> 
    <StackPanel Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Right"> 

     <Button Content="Help" WindowChrome.IsHitTestVisibleInChrome="True" Height="28" Margin="0,0,120,0"/> 
    </StackPanel> 

WPF window

+0

您的窗口是否有內容? – dotNET

+0

已編輯的問題 – murmansk

+0

嘗試兩個快速的事情。首先將你的StackPanel的路線設置爲'Stretch'。其次,使用'GlassFrameThickness'來玩看看是否有任何效果。 – dotNET

回答

1

您需要自己繪製背景。放入ControlTemplateButton和移動ContentPresenter到您指定背景另一行:

<Window x:Class="WpfApplication1.MainWindoq" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="300" Width="300"> 
    <WindowChrome.WindowChrome> 
     <WindowChrome GlassFrameThickness="0.5,28,0,0.5" NonClientFrameEdges="Right"/> 
    </WindowChrome.WindowChrome> 
    <Window.Template> 
     <ControlTemplate> 
      <Grid> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="28" /> 
        <RowDefinition /> 
       </Grid.RowDefinitions> 
       <StackPanel Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Right"> 
        <Button Content="Help" WindowChrome.IsHitTestVisibleInChrome="True" Height="28" Margin="0,0,120,0" 
          Click="Button_Click"/> 
       </StackPanel> 
       <Border Background="White" Grid.Row="1"> 
        <ContentPresenter Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window}, Path=Content}" /> 
       </Border> 
      </Grid> 
     </ControlTemplate> 
    </Window.Template> 
    <Grid> 

    </Grid> 
</Window> 

您看到的黑色部分是未上漆的區域。這是默認的黑色,而不是白色。

0

我認爲這是與您控制模板,請嘗試更改控制模板

<ControlTemplate TargetType="{x:Type local:MainWindow}" > 
      <Grid> 
       <Rectangle Fill="Blue" VerticalAlignment="Top" Width="{TemplateBinding Width}" 
             Height="{TemplateBinding Height}"></Rectangle> 
       <!-- This is the ContentPresenter that displays the window content. --> 
       <Border Margin="0,40,0,5" > 
        <ContentPresenter Content="{TemplateBinding Content}"/> 
       </Border> 
       <!--This is the transparent white rectangle that goes behind the window content.--> 
       <Border Margin="1,40,1,5" BorderBrush="Gray" BorderThickness="0,1,0,1" 
          Grid.ZIndex="-1"> 
        <Rectangle Fill="White" Opacity="0.5" /> 
       </Border> 

       <!-- Window Title --> 
       <TextBlock VerticalAlignment="Top" TextAlignment="Left" 
         Text="{Binding RelativeSource= 
            {RelativeSource TemplatedParent}, Path=Title}" /> 
      </Grid> 
     </ControlTemplate> 
+0

它將覆蓋整個窗口,甚至關閉按鈕 – murmansk

0

1「...」,設置‘控件模板’,只顯示內容
2然後在內容中,網格沒有任何樣式(可能是背景爲空)

做一些測試
添加一個單擊處理按鈕

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    var color = Grid.Background;//is null 
} 
0

不是直接回答你的問題,但你可以實現你後的效果,而無需使用任何第三方解決方案。只需將您的按鈕VerticalAlignment設置爲Top,然後使用負頂端Margin將其移動到標題欄上即可。我已經使用該技術,在過去像這樣創建窗口:但是那

enter image description here

記住在這種情況下,你需要設置你的WindowStyleNoneAllowsTransparencyTrue。如果您的應用程序需要它們,您需要自己繪製系統按鈕。