【问题标题】:Why is my XAML Window partially black at the bottom right corner?为什么我的 XAML 窗口的右下角部分是黑色的?
【发布时间】:2018-07-04 19:07:05
【问题描述】:

我有以下窗口:

<Window x:Class="WpfApplication1.MainWindow"
    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="350" Width="525" WindowStartupLocation="CenterScreen" ShowInTaskbar="True"
    Style="{StaticResource BorderlessWindow}" SizeToContent="WidthAndHeight">

    <Grid>

    </Grid>
</Window>

在我的 App.xaml 中,我有以下无边框窗口样式:

<Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:shell="http://schemas.microsoft.com/winfx/2006/xaml/presentation/shell"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <Style x:Key="BorderlessWindow" TargetType="{x:Type Window}">
            <Setter Property="MinWidth" Value="325" />
            <Setter Property="MinHeight" Value="240" />
            <Setter Property="shell:WindowChrome.WindowChrome">
                <Setter.Value>
                    <shell:WindowChrome CaptionHeight="32"
                                        GlassFrameThickness="0"
                                        ResizeBorderThickness="12" />
                </Setter.Value>
            </Setter>
        </Style>
    </Application.Resources>
</Application>

要让应用程序运行,您需要对 Microsoft.Windows.Shell 的引用。完整地说:我们使用的是 .NET Framework 4.0 和 VS 2015。

我已将SizeToContent="WidthAndHeight" 设置为获得更小的窗口,但是当我这样做时,我会在右侧和底部看到黑色“阴影”。通过 snoop 我看到 MainWindow 设置为 MinHeight 和 MinWidth,但里面的 Border 没有。

为什么内容没有调整大小?当通过鼠标调整窗口大小来更新窗口时,窗口看起来像预期的那样。

【问题讨论】:

    标签: wpf xaml window styles


    【解决方案1】:

    您可以为 ContentRendered 添加一个处理程序,并在呈现内容后强制它重绘。你可以把它挂在 xaml 或后面的代码中,这里是在后面的代码中。

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            ContentRendered += OnContentRendered;
        }
    
        private void OnContentRendered(object sender, EventArgs eventArgs)
        {
            InvalidateVisual();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-23
      • 2015-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-01
      • 2020-11-14
      相关资源
      最近更新 更多