Welcome to WindowsClient.net | Sign in | Join

The Control Gallery is a directory of controls that you can use in your applications. The number in parenthesis indicates how many controls are available in the category.

Dragable Objects

Control Author:

Posted: 05-10-2007

Views: 1,644

Downloads: 673

 
File Details

Simply create a Thumb control and override its template to achieve whatever visual you wish.  In this case I have made the Thumb’s visual an Ellipse.  Then create a handler for the DragDelta event and change the Thumb’s position.  Note that I have chosen to use the Canvas.SetLeft and SetTop properties meaning that the dragable object needs to be contained within a Canvas.

 

<Window x:Class="DragableObject.Window1"

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

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="DragableObject" Height="300" Width="300"

    >

  <Window.Resources>

    <ResourceDictionary>

      <ControlTemplate x:Key="template1">

        <Ellipse Width="60" Height="30" Fill="Black"/>

      </ControlTemplate>

    </ResourceDictionary>

  </Window.Resources>

 

  <Canvas Name="myCanvas">

    <Thumb Name="myThumb" DragDelta="onDragDelta" Canvas.Left="0" Canvas.Top="0" Template="{StaticResource template1}"/>

  </Canvas>

</Window>

 

 

void onDragDelta(object sender, DragDeltaEventArgs e)

{

  Canvas.SetLeft(myThumb, Canvas.GetLeft(myThumb) +

    e.HorizontalChange);

  Canvas.SetTop(myThumb, Canvas.GetTop(myThumb) +

    e.VerticalChange);

}




Page view counter