Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw Shape exclusively inside Canvas

I have a Shape inside Canvas, like this:

<ScrollViewer>

    <Border Height="342" Width="470" HorizontalAlignment="Left" 
        VerticalAlignment="Top" BorderThickness="3" BorderBrush="Black">

        <Canvas Background="White">
            <Rectangle Width="200" Height="200" Canvas.Left="103" 
                Canvas.Top="186" Fill="Red" />
        </Canvas>

    </Border>

</ScrollViewer>

Even if the Rectangle is a Canvas children it's draw outside Canvas limits, covering Border bottom border. How can I make the Rectangle is draw only inside Canvas limits, ensuring that the part of rectangle that lies beyond is not displayed?

Thanks.

like image 397
gliderkite Avatar asked Dec 09 '22 01:12

gliderkite


1 Answers

This is what the ClipToBounds property was made for:

<Canvas Background="White" ClipToBounds="True"> 
    <Rectangle Width="200" Height="200" Canvas.Left="103" Canvas.Top="186" Fill="Red" /> 
</Canvas> 
like image 129
Clemens Avatar answered Dec 23 '22 03:12

Clemens