.net 如何使元素能够在其容器外部绘制?

.net 如何使元素能够在其容器外部绘制?,.net,wpf,xaml,canvas,rendering,.net,Wpf,Xaml,Canvas,Rendering,我试图让元素在其父面板的边界之外进行渲染,在本例中,我使用的是堆栈面板 <StackPanel ClipToBounds="False" Width="200" Orientation="Horizontal" Height="50" Background="{DynamicResource TierBackground}"> <Rectangle ClipToBounds="False" VerticalAlignment="Bottom" W

我试图让元素在其父面板的边界之外进行渲染,在本例中,我使用的是堆栈面板

<StackPanel ClipToBounds="False" Width="200" Orientation="Horizontal" Height="50"
            Background="{DynamicResource TierBackground}">
    <Rectangle ClipToBounds="False" VerticalAlignment="Bottom" Width="25" Height="75"
               Fill="#FF4D6072" />
</StackPanel>

设置
ClipToBounds
似乎没有任何作用,我首先在
矩形上尝试了它,然后在父面板上尝试了它,尽管两者似乎都没有帮助

更新

看起来
Canvas
容器尊重
cliptobunds
属性,但似乎没有其他容器尊重此属性

更新

我已经包括了一张我正在努力实现的图片。棕色区域是在父堆栈面板内分组的内部堆栈面板,请参见灰色框(表示产品定位)如何延伸到父容器之外,并从上面的层覆盖父产品

这是通过在父级
StackPanel
中堆叠多个画布实现的,子产品元素的
canvas.Bottom
属性设置为0。虽然这是可行的,但这意味着我必须将每个产品元素设置为“Left”属性,并且不能让布局自动定位产品


您可以通过设置边距属性来控制渲染。例如,将其设置为负值,以便在stackpanel外部绘制矩形:

<Rectangle ClipToBounds="False" VerticalAlignment="Bottom" Width="25" Height="75"
                   Margin="-50,-50,0,0"
           Fill="#FF4D6072" />

编辑:

下面是一个使用Margin属性生成类似于您的案例的示例:



请注意使用任意大的负数作为边距的技巧,这样您就不必计算边距。

StackPanel
中,这似乎对我不起作用,但如果它起作用,我需要计算对象超出每个项目边界的量。感谢Stanislav,成功地完成了这项工作,非常感谢,这是一种有点奇怪的方式,实际上我还需要做更多的工作才能让它正常工作,因为绑定并不是那么简单。
<Window x:Class="RectangleOutsidePanel.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
    <DockPanel LastChildFill="False">
        <StackPanel Background="LightBlue" Height="50" DockPanel.Dock="Top"/>
        <StackPanel Orientation="Horizontal" Height="50" Background="Brown" DockPanel.Dock="Top">
            <Rectangle 
                VerticalAlignment="Bottom" Width="30" Height="75" Margin="5,-500,5,0"
                Fill="#FF4D6072" />
            <Rectangle 
                VerticalAlignment="Bottom" Width="25" Height="80" Margin="5,-500,5,0"
                Fill="#FF4D6072" />
        </StackPanel>
    </DockPanel>
</Grid>
</Window>