public void TransformToVisual_InVisualTree4 () { // Set explicit widths/heights on all elements so that the values of the MatrixTransform are predictable Rectangle a = new Rectangle { Width = 10, Height = 10, Fill = new SolidColorBrush (Colors.Red) }; StackPanel panel = new StackPanel { Width = 100, Height = 100 }; TestPanel.Width = 200; TestPanel.Height = 200; panel.Children.Add (a); CreateAsyncTest (panel, () => { GeneralTransform m = a.TransformToVisual (TestPanel); Assert.IsTrue (m is MatrixTransform, "#1"); Assert.Matrix (((MatrixTransform) m).Matrix, 1, 0, 0, 1, 95, 50, "#2"); // Fails in Silverlight 3 m = TestPanel.TransformToVisual (a); Assert.IsTrue (m is MatrixTransform, "#3"); Assert.Matrix (((MatrixTransform) m).Matrix, 1, 0, 0, 1, -95, -50, "#4"); }); }
/// <summary> /// Calculate actual layout Rect of Rectangle relative to Viewbox. /// </summary> /// <param name="vb">Containing Viewbox.</param> /// <param name="r">Contained Rectangle.</param> /// <returns>Actual layout Rect of contained Rectangle relative to containing Viewbox.</returns> private static Rect CalculateActualLayout(Viewbox vb, Rectangle r) { Assert.AreEqual(VisualTreeHelper.GetChildrenCount(vb), 1, "Viewbox template changed!"); Assert.IsInstanceOfType(VisualTreeHelper.GetChild(vb, 0), typeof(ContentPresenter), "Viewbox template changed!"); ContentPresenter container = VisualTreeHelper.GetChild(vb, 0) as ContentPresenter; ScaleTransform scale = container.RenderTransform as ScaleTransform; Assert.IsNotNull(scale, "Viewbox implementation changed!"); Point offset = r.TransformToVisual(vb).Transform(new Point(0, 0)); offset = scale.Transform(offset); Rect actual = new Rect(offset.X, offset.Y, r.ActualWidth * scale.ScaleX, r.ActualHeight * scale.ScaleY); return actual; }
public void TransformToVisual_InVisualTree3 () { Rectangle a = new Rectangle { Width = 10, Height = 10, Fill = new SolidColorBrush (Colors.Red) }; Rectangle b = new Rectangle { Width = 10, Height = 10, Fill = new SolidColorBrush (Colors.Blue ) }; StackPanel panel = new StackPanel (); panel.Children.Add (a); panel.Children.Add (b); CreateAsyncTest (panel, () => { GeneralTransform m = a.TransformToVisual (b); Assert.IsTrue (m is MatrixTransform, "#1"); Assert.Matrix (((MatrixTransform) m).Matrix, 1, 0, 0, 1, 0, -10, "#2"); m = b.TransformToVisual (a); Assert.IsTrue (m is MatrixTransform, "#3"); Assert.Matrix (((MatrixTransform) m).Matrix, 1, 0, 0, 1, 0, 10, "#4"); }); }
void ClipImage() { RectangleGeometry geo = new RectangleGeometry(); r = (Rectangle)(from c in LayoutRoot.Children where c.Opacity == 0.5 select c).First(); GeneralTransform gt = r.TransformToVisual(LayoutRoot); Point p = gt.Transform(new Point(0, 0)); geo.Rect = new Rect(p.X, p.Y, r.Width, r.Height); image1.Clip = geo; r.Visibility = System.Windows.Visibility.Collapsed; TranslateTransform t = new TranslateTransform(); t.X = -p.X; t.Y = -p.Y; image1.RenderTransform = t; }