Sunday, September 30

Drag Object with click event inside Window (WPF c# application)

NameSpace: System.Windows.Controls.Primitives.Thumb
Download Here



The Thumb control provides drag functionality that can be used to move or resize controls by monitoring the DragStartedDragDelta and DragCompleted events of the Thumb.
The user begins a drag operation by pressing the left mouse button when the mouse pointer is paused on the Thumb control. The drag operation continues as long as the left mouse button remains pressed. During the drag operation, the DragDelta can occur more than once. Each time it occurs, the DragDeltaEventArgs class provides the change in position that corresponds to the change in mouse position. When the user releases the left mouse button, the drag operation is finished. The drag operation only provides new coordinates; it does not automatically reposition the Thumb.


The following example shows a Thumb control that is the child element of a Canvas control. The event handler for its DragDelta event provides the logic to move the Thumband resize the Canvas. The event handlers for the DragStarted and DragCompleted event change the color of the Thumb during a drag operation. The following example defines the Thumb.

WPF Code:


<Window x:Class="ShapeConnectors.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>

No comments:

Post a Comment