【问题标题】:WPF WindowChrome unwanted black shadow - Caused by SizeToContent="WidthAndHeight"WPF WindowChrome 不需要的黑色阴影 - 由 SizeToContent="WidthAndHeight" 引起
【发布时间】:2019-07-13 21:19:58
【问题描述】:

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

我可以通过删除 SizeToContent="WidthAndHeight" 来删除“阴影”,但是我没有一个窗口可以适应它的内容。

这是 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>

我该如何解决这个问题?

【问题讨论】:

  • 您的应用主窗口是否也有阴影?也许它是 Windows 8/10 的东西?否则,您可以使用 MahApps Metro 之类的东西并禁用阴影属性
  • @MickyD 我不认为这是一个真正的影子。它似乎是弹出窗口的一部分,但由于某种原因画错了。我可以通过拖动黑色部分的边缘来调整弹出窗口的大小,就像我可以通过拖动另一侧来调整大小一样。同样,只需将大小调整一毫米,“阴影”就会消失,弹出窗口看起来应该如此。
  • 你有没有想过这个问题?

标签: wpf xaml


【解决方案1】:

Here(或here,更大胆)是一个解决方法

这是一个很奇怪的问题,但确实是在内容渲染时运行一个事件:

ContentRendered="Window_OnContentRendered"

像这样:

private void Window_OnContentRendered(object sender, EventArgs e)
{
    InvalidateVisual();
}

【讨论】:

    【解决方案2】:

    您可以将 GlassFrameThickness 设置为零:

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

    这应该会移除阴影效果,如果您有兴趣,它还允许您使用 CornerRadius 效果。

    【讨论】:

    • GlassFrameThickness 在厚度为零的情况下会删除窗口阴影,而不是 SizeToContent 工件。 @Shanie 没有说真正的窗户阴影。
    【解决方案3】:

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

    这对我有帮助:

    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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-24
      • 2019-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-27
      相关资源
      最近更新 更多