WPF WindowChrome不必要的黑色阴影-由SizeToContent =“ WidthAndHeight”

时间:2019-07-13 21:19:58

标签: wpf xaml

我正在尝试在WPF中创建一个自定义弹出窗口,但是如果周围没有黑色的“投影”,我似乎无法做到这一点。我将ContentControl用作其主体,以便可以为不同的弹出窗口更改主体。

我可以通过删除SizeToContent =“ WidthAndHeight”来删除“阴影”,但是然后我没有适合其内容的窗口。

enter image description here

这是xaml

<Window x:Class="PriceFinding.Utility.Dialogs.Service.DialogWindow"
    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:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:PriceFinding.Utility.Dialogs.Service"
    mc:Ignorable="d"
    SizeToContent="WidthAndHeight"
    WindowStartupLocation="CenterScreen"
    MinHeight="300" MinWidth="800"
    Background="{StaticResource BrushPrimary}"
    >

     <WindowChrome.WindowChrome>
      <WindowChrome CaptionHeight="50"/>
 </WindowChrome.WindowChrome>

 <Grid>
      <Grid.RowDefinitions>
           <RowDefinition Height="auto"/>
           <RowDefinition Height="*"/>
      </Grid.RowDefinitions>

      <Grid Grid.Row="0"  WindowChrome.IsHitTestVisibleInChrome="True"  VerticalAlignment="Top" Background="{StaticResource BrushPrimaryDark}" Name="TitleBar" Height="35">

                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                     <Image Source = "pf.ico" Height="20" Width="20" Margin="5,0" />
                     <Label Content="{Binding Title}" VerticalAlignment="Center" HorizontalAlignment="Left" FontSize="14"/>
                </StackPanel>

                <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,0,5,0">
                     <Button 
                     x:Name="MinButton" 
                     Height="35" Width="35" Padding="0"
                     Command="{Binding MinimizeButton.MinimizeCommand}">                             
                     </Button>

                     <Button 
                     x:Name="MaxButton" 
                     Height="35" Width="35" Padding="0"
                     HorizontalAlignment="Left"                   
                     Command="{Binding MaximizeButton.MaximizeCommand}">                            
                     </Button>

                     <Button 
                     x:Name="CloseButton"
                     Height="35" Width="35" Padding="0"
                     HorizontalAlignment="Right"                         
                     Command="{Binding CloseButton.CloseCommand}">

                     </Button>
                </StackPanel>

      </Grid>

      <ContentControl Grid.Row="1" x:Name="ContentPresenter" Content="{Binding}"></ContentControl>

 </Grid>

我该如何解决?

3 个答案:

答案 0 :(得分:0)

您可以将GlassFrameThickness设置为零:

<WindowChrome GlassFrameThickness="0" 
                  CaptionHeight="0" />

这应该删除阴影效果,并且如果您感兴趣的话,还可以使用CornerRadius效果。

答案 1 :(得分:0)

Here(或更大胆的here)是解决方法

这是一个很奇怪的问题,但实际上,在呈现内容时运行一个事件:

$poll->users()->detach()

像这样:

ContentRendered="Window_OnContentRendered"

答案 2 :(得分:0)

InvalidateVisual不会重新测量窗口的内容。也无需更改镶边。

这对我有帮助:

protected override void OnContentRendered(EventArgs e)
{
    base.OnContentRendered(e);

    // Content of window may be black in case of SizeToContent is set. 
    // This eliminates the problem. 
    // Do not use InvalidateVisual because it may implicitly break your markup.
    InvalidateMeasure();
}