Welcome to MSDN Blogs Sign in | Join | Help

Maintaining pretenses with the layout system [LayoutTransform functionality updated for Silverlight 2!]

In the introductory post for LayoutTransformControl and the feature-enhancing follow-up, I gave a variety of examples to convey the need for LayoutTransform. I also noted that Silverlight 2 supports only RenderTransform, but went on to demonstrate how it was possible to use RenderTransform to get LayoutTransform behavior. I'm biased, but I happen think LayoutTransform is a pretty fundamental part of application design - and the feedback I've gotten about LayoutTransformControl suggests that at least some of you agree with me. :)

Soon after the Silverlight 2 release candidate became available, a customer contacted me to ask about updating LayoutTransformControl. I've spent some time doing just that and am happy to share a version that works well on the latest Silverlight 2 bits. The complete implementation - along with demo and test applications - can be found in LayoutTransformControl.cs in the ZIP file attached to this post.

The sample application works the same as it did before (with the updated look-and-feel of the new bits):

LayoutTransformControl Sample Application

It remains my belief that LayoutTransformControl for Silverlight 2 is just as powerful as WPF's LayoutTransform. Your application can take advantage of complete LayoutTransform functionality today - even though the Silverlight platform doesn't support LayoutTransform!

Notes:

  • Unfortunately, Silverlight does not yet behave the same as WPF with regard to automatically calling the property changed handler of a Transform-typed DependencyProperty when any of that Transform's members change. I explain how clean this makes things in my previous post, and outline how I worked around the issue by introducing a set of custom "Ltc" transforms that automatically notify their LayoutTransformControl parent of any changes. But with the release of the Silverlight 2 RC, the Transform class can no longer be derived from - which forces the removal of the "Ltc" Transforms and completely invalidates my workaround. :(
  • There are now just two scenarios for the Transforms provided to a LayoutTransformControl:
    1. The Transforms are static - so change notifications are unnecessary and there's no need to worry about this. (As before, the Motivation project demonstrates this scenario.)
    2. The Transforms are dynamic and it is now necessary to make a call to LayoutTransformControl.TransformUpdated() whenever they change so that LayoutTransformControl can find out and update its rendering. (The Sample project has been changed to demonstrate this scenario.)
  • Silverlight 2 has introduced a feature known as "layout rounding" - it's conceptually very similar to WPF's SnapsToDevicePixels property. (I can't find documentation of the new UIElement.UseLayoutRounding property quite yet, but it's discussed briefly in the Breaking Changes document.) Unlike WPF's SnapsToDevicePixels which takes effect silently (as I understand it), Silverlight's UseLayoutRounding (which defaults to True) does not. Specifically, UseLayoutRounding causes some of the intermediate values used during the measure/arrange pass to change while LayoutTransformControl is busy working its magic. Left alone, these unexpected changes appear to LayoutTransformControl as various special-cases of the layout system and cause incorrect layout for certain special angles/scales/etc. based on whether things get rounded up or down. So LayoutTransformControl disables UseLayoutRounding for itself (which avoids bad layout during scaling) and that automatically inherits to its templated Grid (which avoids bad layout during rotation). Consequently, LayoutTransformControl's rendering behavior remains correct at all times.
  • If UseLayoutRounding is necessary for some element within a LayoutTransformControl, simply set it back to True on that element and things should continue to work as desired. But note that LayoutTransformControl does look at the outermost child element (the value of its .Child property) for some of its calculations, so UseLayoutRounding should not be toggled on that element. If necessary, wrap that element in a container (ex: Grid) and it should then be fine to set UseLayoutRounding on it.

My thanks to everyone who has expressed interest in LayoutTransformControl or is actively using it in their projects! I hope this update continues to serve you well as Silverlight 2 approaches final release.

Published Monday, September 29, 2008 1:04 AM by Delay
Filed under: ,

Attachment(s): LayoutTransformControl.zip

Comments

# re: Maintaining pretenses with the layout system [LayoutTransform functionality updated for Silverlight 2!]

Monday, September 29, 2008 8:36 AM by Kieren5

As Mr Burns would say, "Excellent"

# Silverlight Cream for September 29, 2008 -- #382

Tuesday, September 30, 2008 2:01 AM by Community Blogs

Boyan Mihaylov on SL/Amazon, David Hyde with SL Stock Portfolio, Chris Anderson with SL LOB app, Jesse

# re: Maintaining pretenses with the layout system [LayoutTransform functionality updated for Silverlight 2!]

Wednesday, October 15, 2008 7:12 PM by RonnieMeijer

Excellent work.

One addition you should mention is when you're using this with animations.

In your constructor add:

CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);

your event handler

void CompositionTarget_Rendering(object sender, EventArgs e)

{

 LayoutTransformControl.TransformUpdated();

}

# re: Maintaining pretenses with the layout system [LayoutTransform functionality updated for Silverlight 2!]

Thursday, October 16, 2008 3:07 PM by Delay

RonnieMeijer,

Thanks for the feedback! I don't have experience with LayoutTransformControl in that scenario, so I really appreciate the tip!

# re: Maintaining pretenses with the layout system [LayoutTransform functionality updated for Silverlight 2!]

Sunday, October 19, 2008 8:21 PM by kettch

I have an interesting issue with the LayoutTransformControl. In one case I have a custom control that consists only of an image, and a rectangle filled with some solid color. The VisualStateMangager is set to show the rectangle as an overlay on mouseover, and keep it shown if the item is selected. I use an imagebrush based on the original image to clip the overlay rectangle to the same shape as the underlying image. I then wrap the custom control in the a LayoutTransformControl.

The problem comes when I rotate the LayoutTransformControl. The custom control rotates just fine, and the image is seen to turn the specified angle. However, the clipping of the overlay rectangle gets rotated, and then flipped horizontally so it looks like it's X scale has been set to -1.

Any thoughts?

BTW, thanks for this awesome control. I'd be so screwed if it weren't for your great work!

# re: Maintaining pretenses with the layout system [LayoutTransform functionality updated for Silverlight 2!]

Tuesday, October 21, 2008 3:24 PM by Delay

kettch,

Your profile doesn't seem to have contact information, so I can't mail you directly. Do you think you could please put together a simple demonstration of this problem in a new project and send that to me with the "Email" link at the upper right of my blog? I'd love to investigate and see what might be going on.

Thanks much for your help here!

# An unexceptional layout improvement [Two LayoutTransformControl fixes for Silverlight 2!]

Tuesday, November 18, 2008 1:35 PM by Delay's Blog

I'd almost finished patting myself on the back for managing to implement WPF's LayoutTransform on Silverlight

# Having problems with layout? Switch to Plan B! [LayoutTransformControl scenarios for WPF]

Thursday, November 20, 2008 1:52 PM by Delay's Blog

When I first wrote about adding full LayoutTransform fidelity to Silverlight with my LayoutTransformControl

# A rose by any other name... [LayoutTransformControl on track to ship in the Silverlight Toolkit under the name LayoutTransformer!]

Monday, March 02, 2009 3:40 PM by Delay's Blog

I'm a believer in the power of LayoutTransform - so much so that I wrote a control to graft this capability

# A bit more than meets the eye [Easily animate LayoutTransformer with AnimationMediator!]

Thursday, April 09, 2009 4:04 PM by Delay's Blog

I came across a question on the Silverlight Toolkit support forum yesterday asking how to animate the

Anonymous comments are disabled
 
Page view tracker