After building Surface applications over the past year we realized that we’ve learned some things along the way. Sharing some of those lessons learned (and documenting them so we can find them later) seemed like the next step. If you can add to or improve upon what we’ll post in the blog, make sure to leave a comment. We like learning new things.
I’m not big on the obligatory ‘first post,’ so I’ll end the introduction there and get right to it.
For one of our projects I needed to create a circular ScatterViewItem that the user could interact with. It should contain everything in it … that is to say, it should clip its contents. A clipping path was the obvious choice.
So I tried using an EllipseGeometry for the clipping path of the ScatterViewItem:
<s:ScatterViewItem Background="DarkMagenta" Width="80" Height="80">
<EllipseGeometry RadiusX="40" RadiusY="40" Center="40,40"/>
This works great until you try resizing the item. The clipping geometry doesn’t stay in sync with the ScatterViewItem as the size changes. Makes sense since I’m hard-coding the radius and center values.
As the size of the ScatterViewItem changes we need to update the size and center of the EllipseGeometry. The simplest way I found to do this is to override the GetLayoutClip method for the ScatterViewItem.
protected override Geometry GetLayoutClip(Size layoutSlotSize)
return new EllipseGeometry(
new Point(layoutSlotSize.Width / 2, layoutSlotSize.Height / 2),
layoutSlotSize.Width / 2,
layoutSlotSize.Height / 2);
Each time the item goes through its layout logic the custom clip will be applied using the current width and height values for the ScatterViewItem. I created a user control and sub-classed the ScatterViewItem. This made it easy to override the GetLayoutClip method.
Using the new custom ScatterViewItem looks like this:
<c:CustomScatterViewItem Background="Chartreuse" Width="80" Height="80"/>
Now I’m able to resize the circular ScatterViewItem and as I resize it, the clipping path is updated as expected. Plus its bright green, which is nice.
Engineering Director, Surface