Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Silverlight Clipping Fun

0.00/5 (No votes)
10 Aug 2012 1  
Silverlight's support for clipping is definitely inferior.

While Silverlight animation performance may be better than WPF, its support for clipping is definitely inferior.

First off, there are no ClipToBounds property. If you want your canvas to clip its content, you need some extra code.

Then, it turns out that a TextBlock inside a canvas won’t clip itself. Basically, the “Width” property is ignored: the text will take as much space as it sees fit. You can, however, tame a TextBlock by placing it inside a Border. This also gives a benefit of vertical alignment, that bare TextBlock cannot do.

Sample

Screenshot

The sample is generated by this code:

<Border Width="300" Height="300" 
BorderBrush="Blue" BorderThickness="1">
    <Canvas>
        <Border Width="50" BorderBrush="Black" 
        BorderThickness="1" Canvas.Left="30" Canvas.Top="30">
            <TextBlock Text="This TextBLock is 50 pixels wide" />
        </Border>

        <Border Width="300" Height="100" 
        BorderBrush="Brown" BorderThickness="1" 
         Canvas.Left="30" Canvas.Top="80">
            <TextBlock Text="Centered text" 
            HorizontalAlignment="Center" VerticalAlignment="Center" />
        </Border>
        
        <TextBlock Width="10" 
        Text="This TextBlock is 10 pixels wide" Canvas.Left="150" 
         Canvas.Top="250" />
    </Canvas>
</Border>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here