为什么我的面板在小于明确尺寸时会一直夹在面板周围? [英] Why do my panels clip all the way around the panel when made smaller than the explicit size?

查看:19
本文介绍了为什么我的面板在小于明确尺寸时会一直夹在面板周围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能是一个令人困惑的问题标题.

Probably a confusing question title.

带有红色矩形的网格是其外观示例.

The Grid with the Red Rectangle is an example of how it should look.

带有蓝色矩形的网格(未出现在图像中)有一个边距,强制第二个网格比我明确设置的要小.这似乎导致 WPF 翻转并隐藏其排列范围之外的所有内容.

The Grid with the Blue Rectangle (not appearing in the image) has a margin that forces the second grid to be smaller than I've explicitly set it. Which appears to cause WPF to flip out and hide everything outside of it's arranged bounds.

我尝试将剪辑设置为大于网格.

I've tried setting the Clip to be larger than the Grid.

我能够避免这种情况的唯一方法是编写一个自定义面板,用 PositiveInfinity 的约束来测量它的子级,然后以正确的宽度排列子级.这种方法有很多问题.对孩子撒谎可不好.

The only way I've been able to avoid this is to write a custom panel that measures it's children with a constraint of PositiveInfinity, but then arranges the children with the correct width. That method has lots of problems. It's not good to lie to your children.

无论如何,这是代码:

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="NegativeMarginTooMuchClipping.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640"
Height="400">

<Grid>
    <StackPanel Width="600" Height="300">
    <Grid Margin="40,50,60,50" Background="#FFB8B8B8" Width="500" Height="50">
        <Rectangle Fill="Red" HorizontalAlignment="Left" Height="50" VerticalAlignment="Top" Width="50" Margin="0,-50,0,0"/>
    </Grid>
    <Grid Margin="40,50,61,50" Background="#FFB8B8B8" Width="500" Height="50">
        <Rectangle Fill="Blue" HorizontalAlignment="Left" Height="50" VerticalAlignment="Top" Width="50" Margin="0,-50,0,0"/>
    </Grid>
    </StackPanel>
</Grid>

已知问题?我做错了吗?需要更多说明吗?

Known issue? Am I doing it wrong? Need more clarification?

推荐答案

决定剪辑的方式取决于三件事.前两个是 ClipToBoundsClip,但第三个更烦人,而且是 GetLayoutClip.

There are three things that go into to determining how something clips. The first two are ClipToBounds and Clip, but the third is a little more annoying and that is GetLayoutClip.

默认情况下,对于 UIElement,GetLayoutClip 方法将返回 null 或与元素大小相同的 RectangleGeometry,具体取决于 ClipToBounds 属性.FrameworkElement 及其派生类要复杂得多.看看 Reflector/ILSpy,你就会明白我的意思.

By default, for UIElement the GetLayoutClip method will return either null or a RectangleGeometry the same size as the element, depending on the ClipToBounds property. FrameworkElement, and it's derivations, are much more complex though. Take a look in Reflector/ILSpy and you will see what I mean.

不过,您可以覆盖此行为.如果您使用类似以下内容作为蓝色矩形的网格,则它将不再被剪裁:

You can override this behavior though. If you use something like the following as your Grid for the blue rectangle, then it will no longer be clipped:

public class MyGrid : Grid {
    protected override Geometry GetLayoutClip(Size layoutSlotSize) {
        return null;
    }
}

这里有一篇很棒的博客文章.

这篇关于为什么我的面板在小于明确尺寸时会一直夹在面板周围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆