C# WPF背景渲染错误
我想把按钮放在chrome上,我找到了这个解决方案 但它将背景渲染为黑色。我哪里都没有把它设成黑色,它怎么会是黑色的C# WPF背景渲染错误,c#,wpf,C#,Wpf,我想把按钮放在chrome上,我找到了这个解决方案 但它将背景渲染为黑色。我哪里都没有把它设成黑色,它怎么会是黑色的 <WindowChrome.WindowChrome> <WindowChrome GlassFrameThickness="0.5,28,0,0.5" NonClientFrameEdges="Right"/> </WindowChrome.WindowChrome> <Window.Template> <C
<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>
我认为这与您的控件模板有关。请尝试更改控件模板
<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>
1在“…”中,您将“ControlTemplate”设置为仅显示内容2然后在内容中,网格没有任何样式(可能是背景为空) 做一些测试
添加按钮的单击处理程序
private void Button_Click(object sender, RoutedEventArgs e)
{
var color = Grid.Background;//is null
}
不是对您的问题的直接回答,但您可以在不使用任何第三方解决方案的情况下实现您想要的效果。只需将按钮的
垂直对齐
设置为顶部
,然后使用负顶部边距
将其移动到标题栏上即可。我过去曾使用该技术创建如下窗口:
但是请记住,在这种情况下,您需要将
WindowStyle
设置为None
,并将allowtransparency
设置为True
。如果应用程序需要,您需要自己绘制系统按钮。您需要自己绘制背景。将按钮
置于控制模板
中,并将内容演示器
移动到另一行,在该行中指定背景:
<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>
您看到的黑色部分是未上漆的区域。默认情况下,这是黑色而不是白色。它将覆盖整个窗口,甚至“关闭”按钮。您的窗口是否有内容?编辑的问题尝试两件快速的事情。首先将StackPanel的对齐设置为
Stretch
。第二,玩玻璃框厚度看看是否有任何效果。无差异,只有铬长度减少,这是什么