How do I enable an element to paint outside its container?

北慕城南 提交于 2019-12-10 17:21:18

问题


I'm trying to get elements to render outside the bounds of their parent panel, in this case I'm using a stack panel.

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

Setting ClipToBounds doesn't seem to do anything, I first tried it on the Rectangle and then on the parent Panel though neither seemed to help.

UPDATE

It appears the Canvas container honours the ClipToBounds property, but no other container seems to honour this.

UPDATE

I've included an image of what I'm trying to achieve. The brown areas are the inner stack panels which are grouped inside the parent stack panel, see how the gray boxes (representing product positioning) extend past the parent container and cover parent product from tiers above.

This was achieved using multiple canvas' stacked inside a parent StackPanel with child product elements having their Canvas.Bottom property set to 0. While this does work it means I have to set each product elements "Left" property and can't have the layout position the product automatically.

StackPanels http://img263.imageshack.us/img263/8682/stackpanels.jpg


回答1:


You can control the rendering by setting the Margin property. For example, set it to negative value so your rectangle is painted outside the stackpanel:

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

EDIT:

Here's an example for using Margin property to produce something similar to your case:

http://img139.imageshack.us/img139/8357/rectangleoutsidepanel.gif

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

Note the trick of using arbitrarily large negative number for Margin so you don't have to calculate it.



来源:https://stackoverflow.com/questions/1825099/how-do-i-enable-an-element-to-paint-outside-its-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!