Transforming an Object : Transform « Graphics « Silverlight






Transforming an Object

Transforming an Object
  
<UserControl x:Class='SilverlightApplication3.MainPage'
    xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' 
    xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
    xmlns:d='http://schemas.microsoft.com/expression/blend/2008' 
    xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006' 
    mc:Ignorable='d' 
    d:DesignWidth='640' 
    d:DesignHeight='480'>
  <StackPanel>
    <TextBox x:Name="txtMatrixTransform">
      <TextBox.RenderTransform>
        <MatrixTransform>
          <MatrixTransform.Matrix>
            <Matrix  M11="1" M12="0" M21="0" M22="1" OffsetX="0" OffsetY="0"/>
          </MatrixTransform.Matrix>
        </MatrixTransform>
      </TextBox.RenderTransform>
    </TextBox>
    <StackPanel Margin="4,4,0,4" HorizontalAlignment="Left" 
         VerticalAlignment="Stretch" Width="99.4" Grid.Row="1">
      <TextBlock Text="M11:" />
      <TextBlock Text="M12:" />
      <TextBlock Text="M21:" />
      <TextBlock Text="M22:" />
      <TextBlock Text="OffsetX:" />
      <TextBlock Text="OffsetY:" />
      <Button Content="Reset" Click="ResetMatrix"/>
    </StackPanel>
    <StackPanel>
      <TextBox x:Name="txtM11" Text="1" />
      <TextBox x:Name="txtM12" Text="0" />
      <TextBox x:Name="txtM21" Text="0" />
      <TextBox x:Name="txtM22" Text="1" />
      <TextBox x:Name="txtOffsetX" Text="0" />
      <TextBox x:Name="txtOffsetY" Text="0" />
      <Button Height="Auto" Width="Auto" Content="Set MatrixTransform" Click="ApplyMatrix"/>
    </StackPanel>
  </StackPanel>
</UserControl>

//File: Page.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace SilverlightApplication3
{
  public partial class MainPage : UserControl
  {
    public MainPage()
    {
      InitializeComponent();
    }

    private void ApplyMatrix(object sender, RoutedEventArgs e)
    {
      MatrixTransform mt = (MatrixTransform)txtMatrixTransform.RenderTransform;
      try
      {
        Matrix m = new Matrix(Convert.ToDouble(txtM11.Text),
         Convert.ToDouble(txtM12.Text), Convert.ToDouble(txtM21.Text),
         Convert.ToDouble(txtM22.Text), Convert.ToDouble(txtOffsetX.Text),
         Convert.ToDouble(txtOffsetY.Text));
        mt.Matrix = m;
      }
      catch
      {
        txtMatrixTransform.Text = "Invalid-retry:-)";
        ResetMatrix(sender, e);
      }
    }

    private void ResetMatrix(object sender, RoutedEventArgs e)
    {
      txtM11.Text = "1";
      txtM12.Text = "0";
      txtM21.Text = "0";
      txtM22.Text = "1";
      txtOffsetX.Text = "0";
      txtOffsetY.Text = "0";
      MatrixTransform mt = (MatrixTransform)txtMatrixTransform.RenderTransform;
      Matrix m = new Matrix(1, 0, 0, 1, 0, 0);
      mt.Matrix = m;
    }
  }
}

   
    
  








Related examples in the same category

1.Translate transformTranslate transform
2.Transform demoTransform demo
3.Scale transformScale transform
4.Combine Transforms with TransformGroupCombine Transforms with TransformGroup
5.Scale Transform, Rotate Transform in codeScale Transform, Rotate Transform in code