Canvas without Viewbox


   
     

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">

  <Canvas Width="18" Height="18" VerticalAlignment="Center">
    <Ellipse Canvas.Left="1" Canvas.Top="1" Width="16" Height="16" Fill="Yellow" Stroke="Black" />
    <Ellipse Canvas.Left="4.5" Canvas.Top="5" Width="2.5" Height="3" Fill="Black" />
    <Ellipse Canvas.Left="11" Canvas.Top="5" Width="2.5" Height="3" Fill="Black" />
    <Path Data="M 5,10 A 3,3 90 0 0 13,10" Stroke="Black" />
  </Canvas>

</Window>

   
    
    
    
    
     


Positioning on a Canvas


   
     
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  HorizontalAlignment="Center" VerticalAlignment="Center">
    <Canvas Background="Yellow" Width="150" Height="100">
      <TextBlock Canvas.Left="10" Canvas.Top="20">Hello</TextBlock>
      <TextBlock Canvas.Right="10" Canvas.Bottom="20">world!</TextBlock>
    </Canvas>    
</Page>

   
    
    
    
    
     


Nested Canvas


   
    

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      WindowTitle="Canvas Sample">

    <StackPanel>
        <TabControl MinHeight="500" MinWidth="400">
            <TabItem  Header="Canvas" IsSelected="True">
                <Canvas Height="400" Width="400">
                    <Canvas Height="100" Width="100"  Top="0" Left="0">
                        <Rectangle Width="100" Height="100" Fill="red"/>
                    </Canvas>
                    <Canvas Height="100" Width="100" Top="100" Left="100">
                        <Rectangle Width="100" Height="100" Fill="green"/>
                    </Canvas>
                    <Canvas Height="100" Width="100" Top="50" Left="50">
                        <Rectangle Width="100" Height="100" Fill="blue"/>
                    </Canvas>
                </Canvas>
            </TabItem>

        </TabControl>
    </StackPanel>

</Page>

   
    
    
    
     


Add buttons to a Canvas with code


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="250" Width="250" Loaded="Window_Loaded">
    <Canvas Name="canvas1">
    </Canvas>
</Window>
//File:Window.xaml.cs
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 WpfApplication1
{

    public partial class Window1 : Window
    {
        Button button1 = null;
        Button button2 = null;
        Button button3 = null;

        public Window1()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            button1 = new Button { Content = "Button", Width = 70, Height = 23 };
            Canvas.SetLeft(button1, 119);
            Canvas.SetTop(button1, 24);
            canvas1.Children.Add(button1);
            button2 = new Button { Content = "Wider" };
            Canvas.SetLeft(button2, 44);
            Canvas.SetTop(button2, 69);
            canvas1.Children.Add(button2);
            button3 = new Button { Content = "Button" };
            Canvas.SetLeft(button3, 78);
            Canvas.SetTop(button3, 119);
            button3.Padding = new Thickness(10, 2, 10, 2);
            canvas1.Children.Add(button3);
        }
    }
}

   
    
     


Put Button onto a Grid


   
  

<Window x:Class="MyFirstWPFApp.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MyFirstWPFApp" Height="336" Width="343">
    <Grid>
      <Button Click="MyClickEvent"
         VerticalAlignment="Top"
         HorizontalAlignment="Left"
         Grid.Column="0"
         Grid.ColumnSpan="1"
         Grid.Row="0"
         Grid.RowSpan="1"
         Margin="43,61,0,0"
         Width="75"
         Height="23"
         Name="btnGo">Go</Button>
    </Grid>  
</Window>

//File:Window.xaml.cs
using System;
using System.Collections.Generic;
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.Shapes;

namespace MyFirstWPFApp
{
    public partial class Window1 : System.Windows.Window
    {
        private void MyClickEvent(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Elexzandreia the beautiful!", "Message", MessageBoxButton.OK, MessageBoxImage.Hand);
        }

        public Window1()
        {
            InitializeComponent();
        }

    }
}

   
    
     


Button Click event handler


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ButtonClick" Height="300" Width="300">
    <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
      
      <Button Click="ButtonClicked">Button</Button>
      
    </Grid>
</Window>



//File:Window.xaml.cs
using System;
using System.Collections.Generic;
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.Shapes;


namespace WpfApplication1
{
    public partial class Window1 : System.Windows.Window
    {

        public Window1()
        {
            InitializeComponent();
        }
        void ButtonClicked(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Button was clicked");
        }
    }
}

   
    
     


Do event based on button name


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="150" Width="250">
    <StackPanel>

        <StackPanel>
            <StackPanel.Resources>
                <Style TargetType="{x:Type Button}">
                    <Setter Property="Margin" Value="2" />                        
                    <EventSetter Event="Click" Handler="btnShowPopup_Click" />
                </Style>
            </StackPanel.Resources>
            <Button Content="Show Popup" Name="btnShowPopup" />
            <Button Content="Fade Popup" Name="btnFadePopup" />
            <Button Content="Scroll Popup" Name="btnScrollPopup" />
            <Button Content="Slide Popup" Name="btnSlidePopup" />
        </StackPanel>
    </StackPanel>
</Window>

//File:Window.xaml.cs
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;
using System.Windows.Controls.Primitives;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void btnShowPopup_Click(object sender, RoutedEventArgs e)
        {
            if (sender == btnFadePopup)
            {
                Console.WriteLine("fade");
            }
            else if (sender == btnScrollPopup)
            {
                Console.WriteLine("scroll");
            }
            else if (sender == btnSlidePopup)
            {
                Console.WriteLine("slide");
            }
            else
            {
                Console.WriteLine("else");

            }
        }
    }
}