Matrix3D Rotate


   
  

<Window x:Class="WpfApplication1.Matrix3DTransforms"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Matrix3D Transformations" Height="450" Width="300">
  <StackPanel>
    <TextBlock Margin="10,10,5,5" Text="Original Matrix:" />
    <TextBlock Name="tbOriginal" Margin="20,0,5,5" />
    <TextBlock Margin="10,0,5,5" Text="Scale:" />
    <TextBlock Name="tbResult" Margin="20,0,5,5" />

  </StackPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Media3D;

namespace WpfApplication1
{
    public partial class Matrix3DTransforms : Window
    {
        public Matrix3DTransforms()
        {
            InitializeComponent();
            Matrix3D M = new Matrix3D(1, 2, 3, 4,
                                      2, 1, 0, 0,
                                      0, 0, 1, 0,
                                      1, 2, 3, 1);


            tbOriginal.Text = "(" + M.ToString() + ")";

            //Translation: 
            M.Rotate(new Quaternion(new Vector3D(1, 2, 3), 45));
            tbResult.Text = "(" + M.ToString() + ")";
        }
    }
}

   
    
     


Matrix3D TranslatePrepend


   
  

<Window x:Class="WpfApplication1.Matrix3DTransforms"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Matrix3D Transformations" Height="450" Width="300">
  <StackPanel>
    <TextBlock Margin="10,10,5,5" Text="Original Matrix:" />
    <TextBlock Name="tbOriginal" Margin="20,0,5,5" />
    <TextBlock Margin="10,0,5,5" Text="Scale:" />
    <TextBlock Name="tbResult" Margin="20,0,5,5" />

  </StackPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Media3D;

namespace WpfApplication1
{
    public partial class Matrix3DTransforms : Window
    {
        public Matrix3DTransforms()
        {
            InitializeComponent();
            Matrix3D M = new Matrix3D(1, 2, 3, 4,
                                      2, 1, 0, 0,
                                      0, 0, 1, 0,
                                      1, 2, 3, 1);


            tbOriginal.Text = "(" + M.ToString() + ")";

            //Translation: 
            M.TranslatePrepend(new Vector3D(100, 150, 200));
            tbResult.Text = "(" + M.ToString() + ")";
        }
    }
}

   
    
     


Matrix3D Translation


   
  

<Window x:Class="WpfApplication1.Matrix3DTransforms"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Matrix3D Transformations" Height="450" Width="300">
  <StackPanel>
    <TextBlock Margin="10,10,5,5" Text="Original Matrix:" />
    <TextBlock Name="tbOriginal" Margin="20,0,5,5" />
    <TextBlock Margin="10,0,5,5" Text="Scale:" />
    <TextBlock Name="tbResult" Margin="20,0,5,5" />

  </StackPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Media3D;

namespace WpfApplication1
{
    public partial class Matrix3DTransforms : Window
    {
        public Matrix3DTransforms()
        {
            InitializeComponent();
            Matrix3D M = new Matrix3D(1, 2, 3, 4,
                                      2, 1, 0, 0,
                                      0, 0, 1, 0,
                                      1, 2, 3, 1);


            tbOriginal.Text = "(" + M.ToString() + ")";

            //Translation: 
            M.Translate(new Vector3D(100, 150, 200));
            tbResult.Text = "(" + M.ToString() + ")";

        }
    }
}

   
    
     


Matrix3D ScalePrepend


   
  

<Window x:Class="WpfApplication1.Matrix3DTransforms"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Matrix3D Transformations" Height="450" Width="300">
  <StackPanel>
    <TextBlock Margin="10,10,5,5" Text="Original Matrix:" />
    <TextBlock Name="tbOriginal" Margin="20,0,5,5" />
    <TextBlock Margin="10,0,5,5" Text="Scale:" />
    <TextBlock Name="tbResult" Margin="20,0,5,5" />

  </StackPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Media3D;

namespace WpfApplication1
{
    public partial class Matrix3DTransforms : Window
    {
        public Matrix3DTransforms()
        {
            InitializeComponent();
            Matrix3D M = new Matrix3D(1, 2, 3, 4,
                                      2, 1, 0, 0,
                                      0, 0, 1, 0,
                                      1, 2, 3, 1);


            Matrix3D M1 = M;
            tbOriginal.Text = "(" + M.ToString() + ")";

            M = M1; // Reset M to the original matrix. 
            M.ScalePrepend(new Vector3D(0.5, 1.5, 2.5));
            tbResult.Text = "(" + M.ToString() + ")";

        }
    }
}

   
    
     


Matrix3D scale transformation


   
  

<Window x:Class="WpfApplication1.Matrix3DTransforms"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Matrix3D Transformations" Height="450" Width="300">
  <StackPanel>
    <TextBlock Margin="10,10,5,5" Text="Original Matrix:" />
    <TextBlock Name="tbOriginal" Margin="20,0,5,5" />
    <TextBlock Margin="10,0,5,5" Text="Scale:" />
    <TextBlock Name="tbResult" Margin="20,0,5,5" />

  </StackPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Media3D;

namespace WpfApplication1
{
    public partial class Matrix3DTransforms : Window
    {
        public Matrix3DTransforms()
        {
            InitializeComponent();
            Matrix3D M = new Matrix3D(1, 2, 3, 4,
                                      2, 1, 0, 0,
                                      0, 0, 1, 0,
                                      1, 2, 3, 1);


            Matrix3D M1 = M;
            tbOriginal.Text = "(" + M.ToString() + ")";

            //Scale: 
            M.Scale(new Vector3D(0.5, 1.5, 2.5));
            tbResult.Text = "(" + M.ToString() + ")";

        }
    }
}

   
    
     


Change the margins of an element that is within a Grid by XAML and programmatic code


   
  

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="WpfApplication1.Window1"
    Title="Dynamically Change Margin Property Sample">
  <DockPanel Background="White">

    <TextBlock DockPanel.Dock="Top" FontSize="24" FontWeight="Bold">Grid Margin Property Sample</TextBlock>
        <Border Border.Background="LightSteelBlue" Border.BorderThickness="2" Border.BorderBrush="Black" DockPanel.Dock="Top">
            <Grid Name="grid1" Height="400">
              <Grid.ColumnDefinitions>
                <ColumnDefinition/>
              </Grid.ColumnDefinitions>
              <Grid.RowDefinitions>
                <RowDefinition/>
              </Grid.RowDefinitions>
                <TextBlock Name="text1" HorizontalAlignment="Center" Grid.Column="0" Grid.Row="0">Some Text.</TextBlock>
          </Grid>
        </Border>
        <Grid HorizontalAlignment="Center" Width="300" DockPanel.Dock="Top">
        <Grid.RowDefinitions>
          <RowDefinition/>
          <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="Auto"/>
          <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
              <ListBox Grid.Row="0" Grid.Column="1" Width="50" Height="50" VerticalAlignment="Top" SelectionChanged="ChangeMargin">
                  <ListBoxItem>10</ListBoxItem>
                  <ListBoxItem>20</ListBoxItem>
                  <ListBoxItem>30</ListBoxItem>
                  <ListBoxItem>40</ListBoxItem>
                  <ListBoxItem>50</ListBoxItem>
                  <ListBoxItem>60</ListBoxItem>
                  <ListBoxItem>70</ListBoxItem>
                  <ListBoxItem>80</ListBoxItem>
                  <ListBoxItem>90</ListBoxItem>
                  <ListBoxItem>100</ListBoxItem>
              </ListBox>
          </Grid>
      </DockPanel>        
</Window>
//File:Window.xaml.cs


using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Navigation;


namespace WpfApplication1
{
  public partial class Window1 : Window
  {
    public void ChangeMargin(object sender, SelectionChangedEventArgs args)
    {
      ListBoxItem li = ((sender as ListBox).SelectedItem as ListBoxItem);
            ThicknessConverter myThicknessConverter = new ThicknessConverter();
            Thickness th1 = (Thickness)myThicknessConverter.ConvertFromString(li.Content.ToString());
            text1.Margin = th1;
            String st1 = (String)myThicknessConverter.ConvertToString(text1.Margin);
            Console.WriteLine("Margin: " + st1);
    }
  }
}

   
    
     


Set margins, by changing any existing property value for the margin in code-behind with Thickness class


   
  

<StackPanel Name="root"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="WpfApplication1.FEMarginProgrammatic">
  <StackPanel.Resources>
      <Style TargetType="Button">
        <Setter Property="Height" Value="25"/>
        <Setter Property="Width" Value="250"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
        <Setter Property="FontSize" Value="20"/>    
      </Style>
  </StackPanel.Resources>
  <Button Click="OnClick" Margin="10" Name="btn1">Click To See Change!!</Button>
</StackPanel>

//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WpfApplication1
{
    public partial class FEMarginProgrammatic {
        void OnClick(object sender, RoutedEventArgs e)
        {
            Thickness marginThickness = btn1.Margin;
            if(marginThickness.Left == 10)
            {
                 btn1.Margin = new Thickness(60);
            } else {
                 btn1.Margin = new Thickness(10);
            }
        }
    }
}