Get position on a Canvas with Canvas.GetLeft


   
  

<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="Thumb_wcp.Pane1">
  <Canvas Width="100" Height="100" Name="myCanvasStretch">
    <TextBox Name="changes" 
         Width="{Binding ElementName=myCanvasStretch,Path=Width}"  
         Height="{Binding ElementName=myCanvasStretch,Path=Height}" 
         Text="Size: 100, 100"/>
    <Thumb Name="myThumb" Canvas.Left="80" Canvas.Top="80" Background="Blue" 
          Width="20" Height="20" DragDelta="onDragDelta"/>
  </Canvas>
</Canvas>

//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Windows.Media;

namespace Thumb_wcp
{
    public partial class Pane1 : Canvas
    {
        void onDragDelta(object sender, DragDeltaEventArgs e)
        {
            double yadjust = myCanvasStretch.Height + e.VerticalChange;
            double xadjust = myCanvasStretch.Width + e.HorizontalChange;
            if ((xadjust >= 0) &amp;&amp; (yadjust >= 0))
            {
                myCanvasStretch.Width = xadjust;
                myCanvasStretch.Height = yadjust;
                Canvas.SetLeft(myThumb, Canvas.GetLeft(myThumb) + e.HorizontalChange);
                Canvas.SetTop(myThumb, Canvas.GetTop(myThumb) + e.VerticalChange);
                Console.WriteLine(myCanvasStretch.Width);
                Console.WriteLine(myCanvasStretch.Height);
            }
        }

    }
}

   
    
     


Canvas Positioning Properties Sample


   
  


<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="Canvas_Positioning_Properties.Window1"
    Title="Canvas Positioning Properties">
  <StackPanel>
    <Canvas Name="canvas1" Height="300">
        <TextBlock Name="text1" FontWeight="Bold" Canvas.Left="0" Canvas.Right="0" Canvas.Top="0" Canvas.Bottom="0">A TextBlock.</TextBlock>
  </Canvas>

    <ListBox Grid.Column="3" Grid.Row="1" VerticalAlignment="Top" Width="60" Margin="10,0,0,0" SelectionChanged="ChangeLeft">
      <ListBoxItem>Auto</ListBoxItem>      
      <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>
  </StackPanel>        
</Window>
//File:Window.xaml.cs

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

namespace Canvas_Positioning_Properties
{

  public partial class Window1 : Window
  {
    public void ChangeLeft(object sender, SelectionChangedEventArgs args)
    {
      ListBoxItem li = ((sender as ListBox).SelectedItem as ListBoxItem);
      LengthConverter myLengthConverter = new LengthConverter();
      Double db1 = (Double)myLengthConverter.ConvertFromString(li.Content.ToString());
      Canvas.SetLeft(text1, db1);
      Console.WriteLine(myLengthConverter.ConvertToString(Canvas.GetLeft(text1)));
    }


  }
}

   
    
     


Set control position for Canvas


   
  

<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);
        }
    }
}

   
    
     


Create a Zoomable Canvas Control


   
  
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WpfApplication1="clr-namespace:WpfApplication1;assembly="
   Title="WPF" Height="300" Width="300" >

    <Window.Resources>
        <Style TargetType="Button">
            <Setter Property="Width" Value="Auto" />
            <Setter Property="Height" Value="24" />
        </Style>
    </Window.Resources>
    <DockPanel>
        <Slider DockPanel.Dock="Bottom" x:Name="zoomSlider" Minimum="0.1" Maximum="5" Value="1"/>
        <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
            <WpfApplication1:ZoomableCanvasControl x:Name="zoomControl">
                <Canvas.LayoutTransform>
                    <ScaleTransform ScaleX="{Binding Path=Value, ElementName=zoomSlider}"
                                    ScaleY="{Binding Path=Value, ElementName=zoomSlider}"/>
                </Canvas.LayoutTransform>
                <Rectangle Canvas.Top="0" Canvas.Left="0" StrokeThickness="2" Stroke="Red" Width="50" Height="50"/>
                <Rectangle Canvas.Top="50" Canvas.Left="50" StrokeThickness="2" Stroke="Blue" Width="150" Height="150"/>
                <Rectangle Canvas.Top="200" Canvas.Left="200" StrokeThickness="2" Stroke="Green" Width="200" Height="200"/>
            </WpfApplication1:ZoomableCanvasControl>
        </ScrollViewer>

    </DockPanel>
</Window>
//File:Window.xaml.cs

using System.Windows;
using System;
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
    }

    public class ZoomableCanvasControl : Canvas
    {
        static ZoomableCanvasControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ZoomableCanvasControl),new FrameworkPropertyMetadata(typeof(ZoomableCanvasControl)));
        }

        protected override Size MeasureOverride(Size constraint)
        {
            double bottomMost = 0d;
            double rightMost = 0d;

            foreach(object obj in Children)
            {
                FrameworkElement child = obj as FrameworkElement;

                if(child != null)
                {
                    child.Measure(constraint);

                    bottomMost = Math.Max(bottomMost,GetTop(child) +child.DesiredSize.Height);
                    rightMost = Math.Max(rightMost,GetLeft(child) +child.DesiredSize.Width);
                }
            }
            return new Size(rightMost, bottomMost);
        }
    }
}

   
    
     


Create a Scrollable Canvas Control


   
  
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WpfApplication1="clr-namespace:WpfApplication1;assembly="
    Title="WPF" Height="200" Width="400" >

    <Window.Resources>
        <Style TargetType="Button">
            <Setter Property="Width" Value="Auto" />
            <Setter Property="Height" Value="24" />
        </Style>
    </Window.Resources>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="0.5*"/>
            <ColumnDefinition Width="0.5*"/>
        </Grid.ColumnDefinitions>

        <ScrollViewer Grid.Column="0">
            <Canvas>
                <Button Canvas.Top="80" Canvas.Left="80">In View</Button>
                <Button Canvas.Top="300" Canvas.Left="80">Out of view</Button>
            </Canvas>
        </ScrollViewer>

        <ScrollViewer Grid.Column="1">
            <WpfApplication1:ScrollableCanvasControl>
                <Button Canvas.Top="80" Canvas.Left="80">In View</Button>
                <Button Canvas.Top="300" Canvas.Left="80">Out of View</Button>
            </WpfApplication1:ScrollableCanvasControl>
        </ScrollViewer>

    </Grid>
</Window>
//File:Window.xaml.cs

using System.Windows;
using System;
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
    }

    public class ScrollableCanvasControl : Canvas
    {
        static ScrollableCanvasControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ScrollableCanvasControl), new FrameworkPropertyMetadata(typeof(ScrollableCanvasControl)));
        }

        protected override Size MeasureOverride(Size constraint)
        {
            double bottomMost = 0d;
            double rightMost = 0d;

            foreach(object obj in Children)
            {
                FrameworkElement child = obj as FrameworkElement;

                if(child != null)
                {
                    child.Measure(constraint);

                    bottomMost = Math.Max(bottomMost,GetTop(child) +child.DesiredSize.Height);
                    rightMost = Math.Max(rightMost, GetLeft(child) + child.DesiredSize.Width);
                }
            }
            return new Size(rightMost, bottomMost);
        }
    }
}

   
    
     


Canvas PreviewMouseDown action and MouseDown action


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

      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition />
          <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <Canvas PreviewMouseDown="PreviewMouseDownCanvas" MouseDown="MouseDownCanvas"
                Width="20" Height="18" VerticalAlignment="Center">

          <Ellipse x:Name="myEllipse" Canvas.Left="1" Canvas.Top="1" Width="16" Height="16"
                   Fill="Yellow" Stroke="Black" />
        </Canvas>

      </Grid>
    </Button>
  </Grid>
</Window>

//File:Window.xaml.cs

using System;
using System.Windows;
using System.Diagnostics;
using System.Windows.Shapes;
using System.Windows.Input;

namespace WpfApplication1
{
    public partial class Window1 : System.Windows.Window
    {
        public Window1()
        {
            InitializeComponent();

        }
       
        void PreviewMouseDownCanvas(object sender, RoutedEventArgs e){ 
            Debug.WriteLine("PreviewMouseDownCanvas"); 
        }

        void MouseDownCanvas(object sender, RoutedEventArgs e){ 
            Debug.WriteLine("MouseDownCanvas"); 
        }
    }
}

   
    
     


Canvas.SetRight


   
  

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="Canvas_Positioning_Properties.Window1"
    Title="Canvas Positioning Properties">
  <StackPanel>
    <Canvas Name="canvas1" Height="300">
        <TextBlock Name="text1" FontWeight="Bold" Canvas.Left="0" Canvas.Right="0" Canvas.Top="0" Canvas.Bottom="0">A TextBlock.</TextBlock>
  </Canvas>
<ListBox Grid.Column="3" Grid.Row="1" VerticalAlignment="Top" Width="60" Margin="10,0,0,0" SelectionChanged="ChangeRight">
      <ListBoxItem>Auto</ListBoxItem>      
      <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>
  </StackPanel>        
</Window>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Data;

namespace Canvas_Positioning_Properties
{

  public partial class Window1 : Window
  {
    public void ChangeRight(object sender, SelectionChangedEventArgs args)
    {
            ListBoxItem li2 = ((sender as ListBox).SelectedItem as ListBoxItem);
            LengthConverter myLengthConverter = new LengthConverter();
      Double db2 = (Double)myLengthConverter.ConvertFromString(li2.Content.ToString());
      Canvas.SetRight(text1, db2);
      
      Console.WriteLine(myLengthConverter.ConvertToString(Canvas.GetRight(text1)));
    }



  }
}