Click here to Skip to main content
15,881,757 members
Articles / Desktop Programming / WPF

WPF Diagram Designer: Part 1

Rate me:
Please Sign up or sign in to vote.
4.97/5 (297 votes)
23 Aug 2008CPOL7 min read 928.3K   62.4K   680  
Drag, resize and rotate elements on a Canvas
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:s="clr-namespace:DragAndResizeControl">

  <!-- DragThumb Template -->
  <ControlTemplate x:Key="DragThumbTemplate" TargetType="{x:Type s:DragThumb}">
    <Rectangle Fill="Transparent"/>
  </ControlTemplate>

  <!-- ResizeDecorator Template -->
  <ControlTemplate x:Key="ResizeDecoratorTemplate" TargetType="{x:Type Control}">
    <Grid>
      <s:ResizeThumb Height="3" Cursor="SizeNS" Margin="0 -4 0 0"
                     Position="Top"  VerticalAlignment="Top" HorizontalAlignment="Stretch"/>
      <s:ResizeThumb Width="3" Cursor="SizeWE" Margin="-4 0 0 0"
                     Position="Left" VerticalAlignment="Stretch" HorizontalAlignment="Left"/>
      <s:ResizeThumb Width="3" Cursor="SizeWE" Margin="0 0 -4 0"
                     Position="Right" VerticalAlignment="Stretch" HorizontalAlignment="Right"/>
      <s:ResizeThumb Height="3" Cursor="SizeNS" Margin="0 0 0 -4"
                     Position="Bottom" VerticalAlignment="Bottom" HorizontalAlignment="Stretch"/>
      <s:ResizeThumb Width="7" Height="7" Cursor="SizeNWSE" Margin="-6 -6 0 0"
                     Position="TopLeft" VerticalAlignment="Top" HorizontalAlignment="Left"/>
      <s:ResizeThumb Width="7" Height="7" Cursor="SizeNESW" Margin="0 -6 -6 0"
                     Position="TopRight" VerticalAlignment="Top" HorizontalAlignment="Right"/>
      <s:ResizeThumb Width="7" Height="7" Cursor="SizeNESW" Margin="-6 0 0 -6"
                     Position="BottomLeft" VerticalAlignment="Bottom" HorizontalAlignment="Left"/>
      <s:ResizeThumb Width="7" Height="7" Cursor="SizeNWSE" Margin="0 0 -6 -6"
                     Position="BottomRight" VerticalAlignment="Bottom" HorizontalAlignment="Right"/>
    </Grid>
  </ControlTemplate>

  <!-- Draggable only Designer Item -->
  <ControlTemplate x:Key="DragableDesignerItem" TargetType="ContentControl">
    <Grid>
      <s:DragThumb Template="{StaticResource DragThumbTemplate}" Cursor="SizeAll"
                   DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}"/>
      <ContentPresenter Content="{TemplateBinding ContentControl.Content}"/>
    </Grid>
  </ControlTemplate>

  <!-- Resizable only Designer Item -->
  <ControlTemplate x:Key="ResizableDesignerItem" TargetType="ContentControl">
    <Grid>
      <Control Template="{StaticResource ResizeDecoratorTemplate}"
               DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}"/>
      <ContentPresenter Content="{TemplateBinding ContentControl.Content}"/>
    </Grid>
  </ControlTemplate>

  <!-- Dragable and Resizable Designer Item -->
  <ControlTemplate x:Key="DragableAndResizableDesignerItem" TargetType="Control">
    <Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
      <s:DragThumb Template="{StaticResource DragThumbTemplate}" Cursor="SizeAll"/>
      <Control Template="{StaticResource ResizeDecoratorTemplate}"/>
      <ContentPresenter Content="{TemplateBinding ContentControl.Content}"/>
    </Grid>
  </ControlTemplate>

</ResourceDictionary>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions