<Window x:Class="WpfApplication1.Window14" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window14" Height="400" Width="700"> <Canvas x:Name="MainCanvas"> <Canvas Height="200" Width="200" Canvas.Top="50" Canvas.Left="100" Background="Blue"> <Canvas.RenderTransform> <RotateTransform Angle="25"></RotateTransform> </Canvas.RenderTransform> <Canvas Height="140" Width="140" Canvas.Top="30" Canvas.Left="30" Background="Red" MouseDown="Inner_MouseDown"> <Canvas.RenderTransform> <TransformGroup> <RotateTransform Angle="10"/> </TransformGroup> </Canvas.RenderTransform> <Canvas x:Name="Inner" Height="80" Width="80" Canvas.Left="60" Canvas.Top="20" Background="Yellow" MouseDown="Inner_MouseDown"> <Canvas.RenderTransform> <TransformGroup> <RotateTransform Angle="40"></RotateTransform> </TransformGroup> </Canvas.RenderTransform> <TextBlock Canvas.Top="20" Canvas.Left="20" FontSize="20">Hello</TextBlock> </Canvas> </Canvas> </Canvas> <Canvas x:Name="Target" Width="200" Height="200" Canvas.Top="50" Canvas.Left="400" Background="PaleGreen"> <Canvas.RenderTransform> <RotateTransform Angle="10" /> </Canvas.RenderTransform> </Canvas> </Canvas> </Window>As an example when the user drags the canvas named "Inner" to the canvas named "Target" I want to switch the parent of "Inner" to match. Here's simple code to simulate:
private void Inner_MouseDown(object sender, MouseButtonEventArgs e) { Canvas parent = (Canvas) VisualTreeHelper.GetParent(Inner); parent.Children.Remove(Inner); Target.Children.Add(Inner); }However after the operation I want Inner to be displayed at the same overall rotation. What is the most elegant way of re-calculating the new rotation to be applied to "Inner"? (FWIW in practice there will be other transformations applied to the canvas so I guess I need to work with the matrix of a TransformGroup rather than a RotateTransform