Silverlight ClipToBounds

WPF’s ClipToBounds property is not available on Panels in Silverlight, which can lead to some design frustration. Using the new Interactions and Blend Behavior framework, it’s easy to whip up something nearly identical!

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Interactivity;
//using Microsoft.Expression.Interactivity.Core;

namespace AutoClipperDemo
{
    public class AutoClipBehavior : Behavior<DependencyObject>
    {
        private RectangleGeometry _rectGeo;

        protected override void OnAttached()
        {
            base.OnAttached();

            if (base.AssociatedObject is FrameworkElement)
            {                 
                FrameworkElement fe = AssociatedObject as FrameworkElement;
                fe.SizeChanged += new SizeChangedEventHandler(AttachedObjectSizeChanged);
                
                _rectGeo = new RectangleGeometry();
                fe.Clip = _rectGeo;
                
                OnAdjustClip(fe, new Size(fe.ActualWidth, fe.ActualHeight));
            }            
        }

        protected virtual void OnAdjustClip(FrameworkElement fe, Size sz)
        {
            Rect rect = new Rect(0, 0, sz.Width, sz.Height);

            // it would be interesting to handle the border, but alas
            // we can't as it would clip the border as well. :)
            // best solution is to just put the border on or around
            // the panel being clipped by this
            _rectGeo.Rect = rect;
        }

        private void AttachedObjectSizeChanged(object sender, SizeChangedEventArgs e)
        {            
            OnAdjustClip(AssociatedObject as FrameworkElement, e.NewSize);
        }

        // always clean up after yourself ... remove anything that was attached
        // to prevent incorrect clipping and unnecessary event handlers
        protected override void OnDetaching()
        {
            base.OnDetaching();

            if (base.AssociatedObject is FrameworkElement)
            {
                FrameworkElement fe = AssociatedObject as FrameworkElement;
                fe.SizeChanged -= new SizeChangedEventHandler(AttachedObjectSizeChanged);
                fe.Clip = null;
                _rectGeo = null;
            }
        }
    }
}

image

<Border Grid.Column="4" Grid.Row="2" BorderBrush="White" BorderThickness="4" Background="#FF31C525">
    <i:Interaction.Behaviors>
        <local:AutoClipBehavior/>
    </i:Interaction.Behaviors>
    <Grid>
        <Ellipse Fill="#FF09570A" Stroke="Black" Height="250" Margin="91,138,-61,-46" VerticalAlignment="Bottom" Width="250"/>
    </Grid>
</Border>

Software Developers, your Benefit to Society ranking = C

According to Money Magazine, Software Developers Quality of life ratings suggest that the benefit to society of Software Developers is only a “C” grade.

image

Other jobs of interest:

Technical Writer, Benefit to society: B

Software Development Director, Benefit to society: B

Attorney/Lawyer, Benefit to society: B

Securities Trader, Benefit to society: C

Software Product Manager, Benefit to society: C