Layout transform is an interesting concept in XAML development. This of course includes WPF, Silverlight, Silverlight for Windows Phone, WinRT XAML. It is often a nice feature (let’s call it a design feature) to have your text rotated 90 degrees to save vertical space. This is especially true on Windows 8 devices where the standard layout includes more horizontal than vertical space. Usually, one would try to use render transform and rotation, but that doesn’t work as expected.
And it shouldn’t really, because there’s a huge difference between rotating using render and layout transform. Not to reinvent the wheel, here is a great blog post that explains it:
Please notice that this is the article from 2008, and it still works so well because it’s written for WPF. Here’s the essential difference:
… any transformations associated with an elements LayoutTransform property will have an impact on the subsequent Measure and Arrange steps. Whereas a RenderTransform will not have any impact on the layout process and will only effect rendering…
And it’s best explained with two screenshots.
Render transform
Code:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
<StackPanel Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <Button Content="First" /> <Button Content="Second" /> <Button Content="Third" /> <Button Content="Fourth"> <Button.RenderTransform> <RotateTransform Angle="-75" /> </Button.RenderTransform> </Button> <Button Content="Fifth" /> <Button Content="Sixth" /> </StackPanel> |
I added 6 buttons, and rotated one for –75 degrees. And what I got on the screen was not what I would expect. I would expect my button to rotate but not render over the other elements to look so ugly. I would want my button to have it’s own space. For that I need LayoutTransform.
Layout transform
Code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<Page x:Class="Transforming.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Transforming" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:common="using:Transforming.Common" mc:Ignorable="d"> <StackPanel Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <Button Content="First" /> <Button Content="Second" /> <Button Content="Third" /> <common:LayoutTransformer HorizontalAlignment="Left"> <common:LayoutTransformer.LayoutTransform> <RotateTransform Angle="-75"/> </common:LayoutTransformer.LayoutTransform> <Button Content="Fourth" /> </common:LayoutTransformer> <Button Content="Fifth" /> <Button Content="Sixth" /> </StackPanel> </Page> |
I added the whole XAML this time to make you notice that I added common in my list of namespaces in order to include the classes from the Common folder, where LayoutTransformer lives. And what is LayoutTransformer? It’s a class published by Microsoft under Microsoft Public License (Ms-PL) as a part of Silverlight Toolkit. Considering that it’s really easy to switch between Silverlight XAML and WinRT XAML, I modified the LayoutTransformer class to work with WinRT and copied the basic defining style to StandardStyles.xaml, which is a resource dictionary that’s part of the standard WinRT project. Here’s the LayoutTransformer class:
Download Layout Transformer class here
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 |
// (c) Copyright Microsoft Corporation. // This source is subject to the Microsoft Public License (Ms-PL). // Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details. // All other rights reserved. // Un-comment the following line for diagnostic output // #define DIAGNOSTICWRITELINE using System; using System.Diagnostics.CodeAnalysis; using System.Windows; using Windows.Foundation; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media; namespace Transforming.Common { /// <summary> /// Represents a control that applies a layout transformation to its Content. /// </summary> /// <QualityBand>Preview</QualityBand> [TemplatePart(Name = TransformRootName, Type = typeof(Grid))] [TemplatePart(Name = PresenterName, Type = typeof(ContentPresenter))] public sealed class LayoutTransformer : ContentControl { /// <summary> /// Name of the TransformRoot template part. /// </summary> private const string TransformRootName = "TransformRoot"; /// <summary> /// Name of the Presenter template part. /// </summary> private const string PresenterName = "Presenter"; /// <summary> /// Gets or sets the layout transform to apply on the LayoutTransformer /// control content. /// </summary> /// <remarks> /// Corresponds to UIElement.LayoutTransform. /// </remarks> public Transform LayoutTransform { get { return (Transform)GetValue(LayoutTransformProperty); } set { SetValue(LayoutTransformProperty, value); } } /// <summary> /// Identifies the LayoutTransform DependencyProperty. /// </summary> public static readonly DependencyProperty LayoutTransformProperty = DependencyProperty.Register( "LayoutTransform", typeof(Transform), typeof(LayoutTransformer), new PropertyMetadata(null, LayoutTransformChanged)); /// <summary> /// Gets the child element being transformed. /// </summary> private FrameworkElement Child { get { // Preferred child is the content; fall back to the presenter itself return (null != _contentPresenter) ? (_contentPresenter.Content as FrameworkElement ?? _contentPresenter) : null; } } // Note: AcceptableDelta and DecimalsAfterRound work around double arithmetic rounding issues on Silverlight. /// <summary> /// Acceptable difference between two doubles. /// </summary> private const double AcceptableDelta = 0.0001; /// <summary> /// Number of decimals to round the Matrix to. /// </summary> private const int DecimalsAfterRound = 4; /// <summary> /// Root element for performing transformations. /// </summary> private Panel _transformRoot; /// <summary> /// ContentPresenter element for displaying the content. /// </summary> private ContentPresenter _contentPresenter; /// <summary> /// RenderTransform/MatrixTransform applied to _transformRoot. /// </summary> private MatrixTransform _matrixTransform; /// <summary> /// Transformation matrix corresponding to _matrixTransform. /// </summary> private Matrix _transformation; /// <summary> /// Actual DesiredSize of Child element (the value it returned from its MeasureOverride method). /// </summary> private Size _childActualSize = Size.Empty; /// <summary> /// Initializes a new instance of the LayoutTransformer class. /// </summary> public LayoutTransformer() { // Associated default style DefaultStyleKey = typeof(LayoutTransformer); // Can't tab to LayoutTransformer IsTabStop = false; #if SILVERLIGHT // Disable layout rounding because its rounding of values confuses things UseLayoutRounding = false; #endif } /// <summary> /// Builds the visual tree for the LayoutTransformer control when a new /// template is applied. /// </summary> protected override void OnApplyTemplate() { // Apply new template base.OnApplyTemplate(); // Find template parts _transformRoot = GetTemplateChild(TransformRootName) as Grid; _contentPresenter = GetTemplateChild(PresenterName) as ContentPresenter; _matrixTransform = new MatrixTransform(); if (null != _transformRoot) { _transformRoot.RenderTransform = _matrixTransform; } // Apply the current transform ApplyLayoutTransform(); } /// <summary> /// Handles changes to the Transform DependencyProperty. /// </summary> /// <param name="o">Source of the change.</param> /// <param name="e">Event args.</param> private static void LayoutTransformChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) { // Casts are safe because Silverlight is enforcing the types ((LayoutTransformer)o).ProcessTransform((Transform)e.NewValue); } /// <summary> /// Applies the layout transform on the LayoutTransformer control content. /// </summary> /// <remarks> /// Only used in advanced scenarios (like animating the LayoutTransform). /// Should be used to notify the LayoutTransformer control that some aspect /// of its Transform property has changed. /// </remarks> public void ApplyLayoutTransform() { ProcessTransform(LayoutTransform); } /// <summary> /// Processes the Transform to determine the corresponding Matrix. /// </summary> /// <param name="transform">Transform to process.</param> private void ProcessTransform(Transform transform) { // Get the transform matrix and apply it _transformation = RoundMatrix(GetTransformMatrix(transform), DecimalsAfterRound); if (null != _matrixTransform) { _matrixTransform.Matrix = _transformation; } // New transform means re-layout is necessary InvalidateMeasure(); } /// <summary> /// Walks the Transform(Group) and returns the corresponding Matrix. /// </summary> /// <param name="transform">Transform(Group) to walk.</param> /// <returns>Computed Matrix.</returns> private Matrix GetTransformMatrix(Transform transform) { if (null != transform) { // WPF equivalent of this entire method: // return transform.Value; // Process the TransformGroup TransformGroup transformGroup = transform as TransformGroup; if (null != transformGroup) { Matrix groupMatrix = Matrix.Identity; foreach (Transform child in transformGroup.Children) { groupMatrix = MatrixMultiply(groupMatrix, GetTransformMatrix(child)); } return groupMatrix; } // Process the RotateTransform RotateTransform rotateTransform = transform as RotateTransform; if (null != rotateTransform) { double angle = rotateTransform.Angle; double angleRadians = (2 * Math.PI * angle) / 360; double sine = Math.Sin(angleRadians); double cosine = Math.Cos(angleRadians); return new Matrix(cosine, sine, -sine, cosine, 0, 0); } // Process the ScaleTransform ScaleTransform scaleTransform = transform as ScaleTransform; if (null != scaleTransform) { double scaleX = scaleTransform.ScaleX; double scaleY = scaleTransform.ScaleY; return new Matrix(scaleX, 0, 0, scaleY, 0, 0); } // Process the SkewTransform SkewTransform skewTransform = transform as SkewTransform; if (null != skewTransform) { double angleX = skewTransform.AngleX; double angleY = skewTransform.AngleY; double angleXRadians = (2 * Math.PI * angleX) / 360; double angleYRadians = (2 * Math.PI * angleY) / 360; return new Matrix(1, angleYRadians, angleXRadians, 1, 0, 0); } // Process the MatrixTransform MatrixTransform matrixTransform = transform as MatrixTransform; if (null != matrixTransform) { return matrixTransform.Matrix; } // TranslateTransform has no effect in LayoutTransform } // Fall back to no-op transformation return Matrix.Identity; } /// <summary> /// Provides the behavior for the "Measure" pass of layout. /// </summary> /// <param name="availableSize">The available size that this element can give to child elements.</param> /// <returns>The size that this element determines it needs during layout, based on its calculations of child element sizes.</returns> protected override Size MeasureOverride(Size availableSize) { FrameworkElement child = Child; if ((null == _transformRoot) || (null == child)) { // No content, no size return Size.Empty; } //DiagnosticWriteLine("MeasureOverride < " + availableSize); Size measureSize; if (_childActualSize == Size.Empty) { // Determine the largest size after the transformation measureSize = ComputeLargestTransformedSize(availableSize); } else { // Previous measure/arrange pass determined that Child.DesiredSize was larger than believed //DiagnosticWriteLine(" Using _childActualSize"); measureSize = _childActualSize; } // Perform a mesaure on the _transformRoot (containing Child) //DiagnosticWriteLine(" _transformRoot.Measure < " + measureSize); _transformRoot.Measure(measureSize); //DiagnosticWriteLine(" _transformRoot.DesiredSize = " + _transformRoot.DesiredSize); // WPF equivalent of _childActualSize technique (much simpler, but doesn't work on Silverlight 2) // // If the child is going to render larger than the available size, re-measure according to that size // child.Arrange(new Rect()); // if (child.RenderSize != child.DesiredSize) // { // _transformRoot.Measure(child.RenderSize); // } // Transform DesiredSize to find its width/height Rect transformedDesiredRect = RectTransform(new Rect(0, 0, _transformRoot.DesiredSize.Width, _transformRoot.DesiredSize.Height), _transformation); Size transformedDesiredSize = new Size(transformedDesiredRect.Width, transformedDesiredRect.Height); // Return result to allocate enough space for the transformation //DiagnosticWriteLine("MeasureOverride > " + transformedDesiredSize); return transformedDesiredSize; } /// <summary> /// Provides the behavior for the "Arrange" pass of layout. /// </summary> /// <param name="finalSize">The final area within the parent that this element should use to arrange itself and its children.</param> /// <returns>The actual size used.</returns> /// <remarks> /// Using the WPF paramater name finalSize instead of Silverlight's finalSize for clarity /// </remarks> protected override Size ArrangeOverride(Size finalSize) { FrameworkElement child = Child; if ((null == _transformRoot) || (null == child)) { // No child, use whatever was given return finalSize; } //DiagnosticWriteLine("ArrangeOverride < " + finalSize); // Determine the largest available size after the transformation Size finalSizeTransformed = ComputeLargestTransformedSize(finalSize); if (IsSizeSmaller(finalSizeTransformed, _transformRoot.DesiredSize)) { // Some elements do not like being given less space than they asked for (ex: TextBlock) // Bump the working size up to do the right thing by them //DiagnosticWriteLine(" Replacing finalSizeTransformed with larger _transformRoot.DesiredSize"); finalSizeTransformed = _transformRoot.DesiredSize; } //DiagnosticWriteLine(" finalSizeTransformed = " + finalSizeTransformed); // Transform the working size to find its width/height Rect transformedRect = RectTransform(new Rect(0, 0, finalSizeTransformed.Width, finalSizeTransformed.Height), _transformation); // Create the Arrange rect to center the transformed content Rect finalRect = new Rect( -transformedRect.Left + ((finalSize.Width - transformedRect.Width) / 2), -transformedRect.Top + ((finalSize.Height - transformedRect.Height) / 2), finalSizeTransformed.Width, finalSizeTransformed.Height); // Perform an Arrange on _transformRoot (containing Child) //DiagnosticWriteLine(" _transformRoot.Arrange < " + finalRect); _transformRoot.Arrange(finalRect); //DiagnosticWriteLine(" Child.RenderSize = " + child.RenderSize); // This is the first opportunity under Silverlight to find out the Child's true DesiredSize if (IsSizeSmaller(finalSizeTransformed, child.RenderSize) && (Size.Empty == _childActualSize)) { // Unfortunately, all the work so far is invalid because the wrong DesiredSize was used //DiagnosticWriteLine(" finalSizeTransformed smaller than Child.RenderSize"); // Make a note of the actual DesiredSize _childActualSize = new Size(child.ActualWidth, child.ActualHeight); //DiagnosticWriteLine(" _childActualSize = " + _childActualSize); // Force a new measure/arrange pass InvalidateMeasure(); } else { // Clear the "need to measure/arrange again" flag _childActualSize = Size.Empty; } //DiagnosticWriteLine(" _transformRoot.RenderSize = " + _transformRoot.RenderSize); // Return result to perform the transformation //DiagnosticWriteLine("ArrangeOverride > " + finalSize); return finalSize; } /// <summary> /// Compute the largest usable size (greatest area) after applying the transformation to the specified bounds. /// </summary> /// <param name="arrangeBounds">Arrange bounds.</param> /// <returns>Largest Size possible.</returns> [SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity", Justification = "Closely corresponds to WPF's FrameworkElement.FindMaximalAreaLocalSpaceRect.")] private Size ComputeLargestTransformedSize(Size arrangeBounds) { //DiagnosticWriteLine(" ComputeLargestTransformedSize < " + arrangeBounds); // Computed largest transformed size Size computedSize = Size.Empty; // Detect infinite bounds and constrain the scenario bool infiniteWidth = double.IsInfinity(arrangeBounds.Width); if (infiniteWidth) { arrangeBounds.Width = arrangeBounds.Height; } bool infiniteHeight = double.IsInfinity(arrangeBounds.Height); if (infiniteHeight) { arrangeBounds.Height = arrangeBounds.Width; } // Capture the matrix parameters double a = _transformation.M11; double b = _transformation.M12; double c = _transformation.M21; double d = _transformation.M22; // Compute maximum possible transformed width/height based on starting width/height // These constraints define two lines in the positive x/y quadrant double maxWidthFromWidth = Math.Abs(arrangeBounds.Width / a); double maxHeightFromWidth = Math.Abs(arrangeBounds.Width / c); double maxWidthFromHeight = Math.Abs(arrangeBounds.Height / b); double maxHeightFromHeight = Math.Abs(arrangeBounds.Height / d); // The transformed width/height that maximize the area under each segment is its midpoint // At most one of the two midpoints will satisfy both constraints double idealWidthFromWidth = maxWidthFromWidth / 2; double idealHeightFromWidth = maxHeightFromWidth / 2; double idealWidthFromHeight = maxWidthFromHeight / 2; double idealHeightFromHeight = maxHeightFromHeight / 2; // Compute slope of both constraint lines double slopeFromWidth = -(maxHeightFromWidth / maxWidthFromWidth); double slopeFromHeight = -(maxHeightFromHeight / maxWidthFromHeight); if ((0 == arrangeBounds.Width) || (0 == arrangeBounds.Height)) { // Check for empty bounds computedSize = new Size(arrangeBounds.Width, arrangeBounds.Height); } else if (infiniteWidth && infiniteHeight) { // Check for completely unbound scenario computedSize = new Size(double.PositiveInfinity, double.PositiveInfinity); } else if (!MatrixHasInverse(_transformation)) { // Check for singular matrix computedSize = new Size(0, 0); } else if ((0 == b) || (0 == c)) { // Check for 0/180 degree special cases double maxHeight = (infiniteHeight ? double.PositiveInfinity : maxHeightFromHeight); double maxWidth = (infiniteWidth ? double.PositiveInfinity : maxWidthFromWidth); if ((0 == b) && (0 == c)) { // No constraints computedSize = new Size(maxWidth, maxHeight); } else if (0 == b) { // Constrained by width double computedHeight = Math.Min(idealHeightFromWidth, maxHeight); computedSize = new Size( maxWidth - Math.Abs((c * computedHeight) / a), computedHeight); } else if (0 == c) { // Constrained by height double computedWidth = Math.Min(idealWidthFromHeight, maxWidth); computedSize = new Size( computedWidth, maxHeight - Math.Abs((b * computedWidth) / d)); } } else if ((0 == a) || (0 == d)) { // Check for 90/270 degree special cases double maxWidth = (infiniteHeight ? double.PositiveInfinity : maxWidthFromHeight); double maxHeight = (infiniteWidth ? double.PositiveInfinity : maxHeightFromWidth); if ((0 == a) && (0 == d)) { // No constraints computedSize = new Size(maxWidth, maxHeight); } else if (0 == a) { // Constrained by width double computedHeight = Math.Min(idealHeightFromHeight, maxHeight); computedSize = new Size( maxWidth - Math.Abs((d * computedHeight) / b), computedHeight); } else if (0 == d) { // Constrained by height double computedWidth = Math.Min(idealWidthFromWidth, maxWidth); computedSize = new Size( computedWidth, maxHeight - Math.Abs((a * computedWidth) / c)); } } else if (idealHeightFromWidth <= ((slopeFromHeight * idealWidthFromWidth) + maxHeightFromHeight)) { // Check the width midpoint for viability (by being below the height constraint line) computedSize = new Size(idealWidthFromWidth, idealHeightFromWidth); } else if (idealHeightFromHeight <= ((slopeFromWidth * idealWidthFromHeight) + maxHeightFromWidth)) { // Check the height midpoint for viability (by being below the width constraint line) computedSize = new Size(idealWidthFromHeight, idealHeightFromHeight); } else { // Neither midpoint is viable; use the intersection of the two constraint lines instead // Compute width by setting heights equal (m1*x+c1=m2*x+c2) double computedWidth = (maxHeightFromHeight - maxHeightFromWidth) / (slopeFromWidth - slopeFromHeight); // Compute height from width constraint line (y=m*x+c; using height would give same result) computedSize = new Size( computedWidth, (slopeFromWidth * computedWidth) + maxHeightFromWidth); } // Return result //DiagnosticWriteLine(" ComputeLargestTransformedSize > " + computedSize); return computedSize; } /// <summary> /// Returns true if Size a is smaller than Size b in either dimension. /// </summary> /// <param name="a">Second Size.</param> /// <param name="b">First Size.</param> /// <returns>True if Size a is smaller than Size b in either dimension.</returns> private static bool IsSizeSmaller(Size a, Size b) { // WPF equivalent of following code: // return ((a.Width < b.Width) || (a.Height < b.Height)); return ((a.Width + AcceptableDelta < b.Width) || (a.Height + AcceptableDelta < b.Height)); } /// <summary> /// Rounds the non-offset elements of a Matrix to avoid issues due to floating point imprecision. /// </summary> /// <param name="matrix">Matrix to round.</param> /// <param name="decimals">Number of decimal places to round to.</param> /// <returns>Rounded Matrix.</returns> private static Matrix RoundMatrix(Matrix matrix, int decimals) { return new Matrix( Math.Round(matrix.M11, decimals), Math.Round(matrix.M12, decimals), Math.Round(matrix.M21, decimals), Math.Round(matrix.M22, decimals), matrix.OffsetX, matrix.OffsetY); } /// <summary> /// Implements WPF's Rect.Transform on Silverlight. /// </summary> /// <param name="rect">Rect to transform.</param> /// <param name="matrix">Matrix to transform with.</param> /// <returns>Bounding box of transformed Rect.</returns> private static Rect RectTransform(Rect rect, Matrix matrix) { // WPF equivalent of following code: // Rect rectTransformed = Rect.Transform(rect, matrix); Point leftTop = matrix.Transform(new Point(rect.Left, rect.Top)); Point rightTop = matrix.Transform(new Point(rect.Right, rect.Top)); Point leftBottom = matrix.Transform(new Point(rect.Left, rect.Bottom)); Point rightBottom = matrix.Transform(new Point(rect.Right, rect.Bottom)); double left = Math.Min(Math.Min(leftTop.X, rightTop.X), Math.Min(leftBottom.X, rightBottom.X)); double top = Math.Min(Math.Min(leftTop.Y, rightTop.Y), Math.Min(leftBottom.Y, rightBottom.Y)); double right = Math.Max(Math.Max(leftTop.X, rightTop.X), Math.Max(leftBottom.X, rightBottom.X)); double bottom = Math.Max(Math.Max(leftTop.Y, rightTop.Y), Math.Max(leftBottom.Y, rightBottom.Y)); Rect rectTransformed = new Rect(left, top, right - left, bottom - top); return rectTransformed; } /// <summary> /// Implements WPF's Matrix.Multiply on Silverlight. /// </summary> /// <param name="matrix1">First matrix.</param> /// <param name="matrix2">Second matrix.</param> /// <returns>Multiplication result.</returns> private static Matrix MatrixMultiply(Matrix matrix1, Matrix matrix2) { // WPF equivalent of following code: // return Matrix.Multiply(matrix1, matrix2); return new Matrix( (matrix1.M11 * matrix2.M11) + (matrix1.M12 * matrix2.M21), (matrix1.M11 * matrix2.M12) + (matrix1.M12 * matrix2.M22), (matrix1.M21 * matrix2.M11) + (matrix1.M22 * matrix2.M21), (matrix1.M21 * matrix2.M12) + (matrix1.M22 * matrix2.M22), ((matrix1.OffsetX * matrix2.M11) + (matrix1.OffsetY * matrix2.M21)) + matrix2.OffsetX, ((matrix1.OffsetX * matrix2.M12) + (matrix1.OffsetY * matrix2.M22)) + matrix2.OffsetY); } /// <summary> /// Implements WPF's Matrix.HasInverse on Silverlight. /// </summary> /// <param name="matrix">Matrix to check for inverse.</param> /// <returns>True if the Matrix has an inverse.</returns> private static bool MatrixHasInverse(Matrix matrix) { // WPF equivalent of following code: // return matrix.HasInverse; return (0 != ((matrix.M11 * matrix.M22) - (matrix.M12 * matrix.M21))); } } } |
The style part:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<Style TargetType="common:LayoutTransformer"> <Setter Property="Foreground" Value="#FF000000"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="common:LayoutTransformer"> <Grid x:Name="TransformRoot" Background="{TemplateBinding Background}"> <ContentPresenter x:Name="Presenter" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> |
Here’s the original code from Silverlight toolkit:
http://silverlight.codeplex.com/SourceControl/changeset/view/74775?ProjectName=silverlight#779405
Enjoy layout transforming in WinRT! :)
1- Brown seaweed extract: It’s this pure ingredient that helps to scale back your appetite and therefore limits your general calorie consumption every day. Meratol weight loss capsule is medically confirmed to burn up your calories as well as in addition, it manages meals and snacks craving so there won’t be the sensation of
consuming unnecessarily. It is known to effectively increase size of breast
in most women.
I think the admin of this website is genuinely working hard in favor of his
web site, since here every data is quality based
information.
Hi igrali ….
I have a problem when user common:LayoutTransformer and ScaleTransform
ScaleTransform can’t work with CenterX and CenterY. Can you fix it ?
Hi igrali,
is there a way to use bindings in your layout transformer? I tried this
but it didn’t work out the way I need it. If the value of AngleObject changes the RotateTransform should be reapplied (in my case to an Image).