2017-08-24 68 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。如果您的应用程序需要它们,您需要自己绘制系统按钮。