<Window x:Class="WpfApplication1.Window23" > xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" > xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" > > <StackPanel Orientation="Vertical"> > <Canvas x:Name="Outer" Height="500" Width="500" Background="AliceBlue"> > <Canvas x:Name="Main" Height="200" Width="200" Background="Red" Canvas.Top="200" ClipToBounds="True" > > <Canvas.RenderTransform> > <RotateTransform Angle="345" /> > </Canvas.RenderTransform> > <Canvas x:Name="Contained" Height="300" Width="300" Background="Yellow" Canvas.Top="50" Canvas.Left="50"> > <Canvas.RenderTransform> > <RotateTransform Angle="345"/> > </Canvas.RenderTransform> > </Canvas> > </Canvas> > </Canvas> > <Border BorderBrush="Black" BorderThickness="2" Width="96" Height="96"> > <Viewbox x:Name="VBox" > > </Viewbox> > </Border> > <Button Click="Button_Click">Thumbnail it</Button> > </StackPanel> ></Window>Code:
using System.Windows; >using System.Windows.Controls; >using System.Windows.Media; > >namespace WpfApplication1 >{ > > public partial class Window23 : Window > { > public Window23() > { > InitializeComponent(); > } > > private void Button_Click(object sender, RoutedEventArgs e) > { > Outer.Children.Remove(Main); > Main.LayoutTransform = Main.RenderTransform; > Main.RenderTransform = Transform.Identity; > VBox.Child = Main; > } > } >}This does what I want. However if ClipToBounds is not set on Main then the sizing is obviously wrong. Whats the simplest way of getting this right?
<Window x:Class="WpfApplication2.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" SizeToContent="WidthAndHeight"> <StackPanel Orientation="Vertical"> <Canvas x:Name="Outer" Height="500" Width="500" Background="AliceBlue"> <Canvas x:Name="cvsMeasure" Height="0" Width="0" Canvas.Top="200" > <Canvas x:Name="Main" Height="200" Width="200" Background="Red" > <Canvas.RenderTransform> <RotateTransform Angle="345" /> </Canvas.RenderTransform> <Canvas x:Name="Contained" Height="300" Width="300" Background="Yellow" Canvas.Top="50" Canvas.Left="50" > <Canvas.RenderTransform> <RotateTransform Angle="345"/> </Canvas.RenderTransform> </Canvas> </Canvas> </Canvas> </Canvas> <Border BorderBrush="Black" BorderThickness="2" Width="96" Height="96"> <Viewbox x:Name="VBox" > <Canvas x:Name="VBoxCanvas" Background="AliceBlue" ></Canvas> </Viewbox> </Border> <Button Click="Button_Click">Thumbnail it</Button> </StackPanel> </Window>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApplication2 { /// <summary> /// Interaction logic for Window1.xaml /// </summary> public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { //Main.LayoutTransform = Main.RenderTransform; //Main.RenderTransform = Transform.Identity; Rect bounds = VisualTreeHelper.GetDescendantBounds(cvsMeasure); Double size = Math.Max(bounds.Size.Width + (bounds.Left*2) , bounds.Size.Height + bounds.Top); cvsMeasure.Children.Remove(Main); Main.SetValue(Canvas.TopProperty, 0-bounds.Top ); Main.SetValue(Canvas.LeftProperty, 0 - bounds.Left); VBoxCanvas.Width = size; VBoxCanvas.Height = size; VBoxCanvas.Children.Add(Main); //VBox.Child = Main; } } }