Monday, October 29, 2012

transforming the Clip of a UIElement

transforming the Clip of a UIElement

I am using C#, Silverlight, Visual Studio for Windows Phone 7.

I would like to get the Clip of a UIElement that includes any transform that has been done on the UIElement.

This example gives me the Clip but does not include any transform from the UIElement:

// uie is a UIElement taken from a PhoneApplicationPage Geometry myClip = uie.Clip; 

I tried the following, but it ended up moving the UIElement:

var frame = Application.Current.RootVisual as PhoneApplicationFrame; var page = frame.Content as PhoneApplicationPage; GeneralTransform gt = uie.TransformToVisual(page); uie.RenderTransform = gt as Transform; Geometry myClip = uie.Clip; 

I also tried to add an inverse transform at the end to undo any movement, but that seemed to make it worse:

uie.RenderTransform = gt.Inverse as Transform; 

Please help me get the Clip with the original transform of the UIElement without messing with the UIElement itself.

Thanks in advance.

Answers & Comments...

Answer: 1

I figured out how to solve this without messing with the original UIElement. I needed to create a new Geometry object with the same data rather than using the original Geometry. Not only does this separate the new data from the old data, it also prevents an object from possibly getting assigned as a child to multiple objects.

Here is the resulting code I used:

var frame = Application.Current.RootVisual as PhoneApplicationFrame; var page = frame.Content as PhoneApplicationPage; GeneralTransform gt = uie.TransformToVisual(page); var place = gt.Transform(new Point()); // declaring new variables to hold these values Geometry geom; Path path = new Path(); // here I checked for other restrictions on my UIElements before assigning the variables geom = new RectangleGeometry(); (geom as RectangleGeometry).Rect = new Rect(place.X, place.Y, uie.RenderSize.Width, uie.RenderSize.Height); path.Data = geom; 

I mentioned that I check for some restrictions on my UIElements before assigning the variables. This is related to another question I posted about clips (here).

by : joulesmhttp://stackoverflow.com/users/610599




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog