Posted by: Zeeshan Amjad | November 13, 2009

Using MatrixTransform


WPF already provide few classes for quick transformation such as rotate transformation, scale transformation, skew transformation, translate transformation etc. But there is one even more low level class available in WPF named MatrixTransform. This class gives the feature of applying more than one transformation at a time if we know how to use matrix. Here is a class diagram of all of the transformations.

GeneralTransform

Input of MatrixTransform  is a Matrix structure. This is a small utility structure to define all the basic operations of the Matrix. Here is a class diagram of this structure.

Matrix

Here is a simple program to use Matrix Transformation.

  1: using System;
  2: using System.Collections.Generic;
  3: using System.Linq;
  4: using System.Text;
  5: using System.Windows;
  6: using System.Windows.Controls;
  7: using System.Windows.Data;
  8: using System.Windows.Documents;
  9: using System.Windows.Input;
 10: using System.Windows.Media;
 11: using System.Windows.Media.Imaging;
 12: using System.Windows.Navigation;
 13: using System.Windows.Shapes;
 14: 
 15: namespace UIElement
 16: {
 17:     /// <summary>
 18:     /// Interaction logic for Window1.xaml
 19:     /// </summary>
 20:     public partial class Window1 : Window
 21:     {
 22:         public List<String> items
 23:         { get; set; }
 24: 
 25:         public Window1()
 26:         {            
 27:             InitializeComponent();
 28: 
 29:             items = new List<string>();
 30:             items.Add("Maryland");
 31:             items.Add("California");
 32:             items.Add("Taxes");
 33:             items.Add("Washington");
 34:             items.Add("Virginia");
 35: 
 36:             list.ItemsSource = items;
 37: 
 38:             Matrix mt = new Matrix(1, 0, 0.5, 1, 0, 0);
 39:             list.LayoutTransform = new MatrixTransform(mt);
 40:         }
 41:     }
 42: }
 43: 

Here is XAML code for this.

  1: <Window x:Class="UIElement.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     xmlns:local="clr-namespace:UIElement"
  5:     Title="Matrix Transformation List Box" Height="300" Width="400">
  6:     <Grid>        
  7:         <ListBox Name="list" Margin="5" HorizontalContentAlignment="Stretch">
  8:             <ListBox.ItemTemplate>
  9:                 <DataTemplate>
 10:                     <Border Margin="5" BorderBrush="Brown" CornerRadius="5"  BorderThickness="2">
 11:                         <TextBlock Margin="5" FontSize="14" Text="{Binding}"/>
 12:                     </Border>
 13:                 </DataTemplate>
 14:             </ListBox.ItemTemplate>
 15:         </ListBox>
 16:     </Grid>
 17: </Window>
 18: 

Here is the output of this program.

MatrixTransformation

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: