Set control position for Canvas

image_pdfimage_print


   
  

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

   
    
     


Canvas Positioning Properties Sample

image_pdfimage_print


   
  


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


  }
}

   
    
     


Get position on a Canvas with Canvas.GetLeft

image_pdfimage_print


   
  

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

    }
}

   
    
     


Three-State CheckBox

image_pdfimage_print


   
    

<Window x:Class="SimpleStyles.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="SimpleStyles"
  Background="#F8F8F8">
  <ScrollViewer>
    <WrapPanel>
      <!-- CheckBox -->
      <HeaderedItemsControl Header="CheckBox">
        <CheckBox Margin="8">Normal</CheckBox>
        <CheckBox Margin="8" IsChecked="true">Checked</CheckBox>
        <CheckBox Margin="8" IsThreeState="true" IsChecked="{x:Null}">Indeterminate</CheckBox>
      </HeaderedItemsControl>
   
    </WrapPanel>
  </ScrollViewer>
</Window>

   
    
    
    
     


A CheckBox with a skew transformation

image_pdfimage_print


   
    

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:sys="clr-namespace:System;assembly=mscorlib" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
  <Grid>

    <Button Content ="Click Me!" Grid.Row="0" Grid.Column="1" Width="95" Height="40">
      <Button.RenderTransform>
        <SkewTransform AngleX ="20" AngleY ="20"/>
      </Button.RenderTransform>
    </Button>

  </Grid>

</Window>

   
    
    
    
     


Add CheckBox to StackPanel

image_pdfimage_print


   
    

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  HorizontalAlignment="Center" VerticalAlignment="Center">
    <StackPanel Background="#ECE9D8">
      <TextBlock Margin="3">Look for:</TextBlock>
      <ComboBox  Margin="3"/>
      <TextBlock Margin="3">Filtered by:</TextBlock>
      <ComboBox  Margin="3"/>
      <Button    Margin="3,5">Search</Button>
      <CheckBox  Margin="3">Search in titles only</CheckBox>
      <CheckBox  Margin="3">Match related words</CheckBox>
      <CheckBox  Margin="3">Search in previous results</CheckBox>
      <CheckBox  Margin="3">Highlight search hits (in topics)</CheckBox>
    </StackPanel>

</Window>

   
    
    
    
     


Customized CheckBox

image_pdfimage_print


   
    
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Page.Resources>
        <ControlTemplate x:Key="switch" TargetType="{x:Type CheckBox}">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Canvas Background="LightGray">
                    <Line Name="lineOff"
                              StrokeThickness="8" Stroke="Black"
                              X1="48" Y1="40" X2="20" Y2="16"
                              StrokeStartLineCap="Round"
                              StrokeEndLineCap="Round"  />
                    <Line Name="lineOn"
                              StrokeThickness="8" Stroke="Black"
                              X1="48" Y1="40" X2="76" Y2="16"
                              StrokeStartLineCap="Round"
                              StrokeEndLineCap="Round" 
                              Visibility="Hidden"  />
                </Canvas>
                <ContentPresenter Grid.Row="1" 
                                  Content="{TemplateBinding Content}"
                                  HorizontalAlignment="Center" />
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Setter TargetName="lineOff" Property="Visibility"
                            Value="Hidden" />
                    <Setter TargetName="lineOn" Property="Visibility"
                            Value="Visible" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Page.Resources>

    <CheckBox Template="{StaticResource switch}"
              Content="Customized CheckBox"
              HorizontalAlignment="Center"
              VerticalAlignment="Center" />
</Page>