BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Presentation Foundation
.NET 4.0+

WPF Base Classes - Thumb

The thirty-seventh part of the Windows Presentation Foundation Fundamentals tutorial looks at another WPF control base type. This time it is the Thumb class, which defines functionality that is used by controls that the user can drag.

DragDelta Event

The third dragging event is DragDelta. This event can be raised many times during a single drag operation. As the mouse is moved, the event is raised repeatedly. Each time it is raised, the event arguments, which are provided as a DragDeltaEventArgs object, describe the movement since the last time a drag event was triggered. The distance travelled by the mouse since the last event can be read from the HorizontalChange and VerticalChange properties. NB: It is possible for either property to return zero. For example, the VerticalChange will be zero if the mouse is moved only horizontally.

To demonstrate the event, modify the XAML for the GridSplitter as shown below:

<GridSplitter Name="MyGridSplitter"
              Background="Gray"
              Width="5"
              HorizontalAlignment="Right"
              VerticalAlignment="Stretch"
              DragStarted="MyGridSplitter_DragStarted"
              DragCompleted="MyGridSplitter_DragCompleted"
              DragDelta="MyGridSplitter_DragDelta"/>

Now add the following code behind the window. This updates the text in the right hand grid column to indicate the direction of horizontal travel for the last dragging movement. This text is updated repeatedly as you move the GridSplitter.

private void MyGridSplitter_DragDelta(
    object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
{
    if (e.HorizontalChange == 0)
        RightText.Text = "Not Moving";
    else if (e.HorizontalChange > 0)
        RightText.Text = "Moving Right";
    else
        RightText.Text = "Moving Left";
}

IsDragging Property

The Thumb class includes a single property. This Boolean property, named IsDragging, returns true whilst the user is dragging the control. If a drag operation is not in progress, it returns false. This property is particularly useful when using data binding or property triggers, which we will see later in this tutorial.

CancelDrag Method

The final member of Thumb that we'll consider in this article is the CancelDrag method. if you call this method, the dragging operation is immediately ceased. The DragCompleted event is raised on cancellation, with the Canceled property of the event arguments set to true. You can check this property and modify the behaviour of your software according to whether the drag was cancelled or ended normally.

For the final example, we'll modify the DragDelta event code. We'll check the position of the GridSplitter by reading the width of the left column's TextBlock. When it reaches a width greater than 200, we'll cancel the dragging.

Modify the code for the event as follows:

private void MyGridSplitter_DragDelta(
    object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
{
    if (e.HorizontalChange == 0)
        RightText.Text = "Not Moving";
    else if (e.HorizontalChange > 0)
        RightText.Text = "Moving Right";
    else
        RightText.Text = "Moving Left";

    if (LeftText.ActualWidth > 200 && RightText.Text == "Moving Right")
    {
        MyGridSplitter.CancelDrag();
    }
}

Run the program and try dragging the GridSplitter to see the results. You should find that if you try to widen the first column too far, the drag is cancelled and further movement of the mouse does not move the control.

23 November 2013