Task Class

Represents a collection of commands and input bindings to those commands.

System.Object
  Microsoft.Windows.Design.Interaction.Task

Namespace:  Microsoft.Windows.Design.Interaction
Assembly:  Microsoft.Windows.Design.Interaction (in Microsoft.Windows.Design.Interaction.dll)

public class Task

The Task type exposes the following members.

  NameDescription
Public methodTaskInitializes a new instance of the Task class.
Top

  NameDescription
Public propertyAdornerFilterGets or sets the filter that is used to filter the set of adorners seen by the designer's hit test algorithm.
Public propertyCommandBindingsGets the CommandBindingCollection for a task.
Public propertyCursorGets or sets the cursor for a task.
Public propertyDescriptionGets or sets the description for this task.
Public propertyInputBindingsGets the InputBindingCollection for a task.
Public propertyIsFocusedGets a value indicating whether this task has the focus.
Public propertyModelFilterGets or sets the filter that is used to filter the set of model items seen by the designer's hit test algorithm.
Public propertyToolCommandBindingsGets the ToolCommandBindingCollection for a task.
Top

  NameDescription
Public methodBeginFocusBegins to set the focus for the task.
Public methodCompleteCompletes changes that were made while this task has the focus.
Public methodEqualsDetermines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodOnCompletedRaises the Completed event.
Protected methodOnFocusDeactivatedRaises the FocusDeactivated event.
Protected methodOnRevertedRaises the Reverted event.
Public methodRevertReverts this task.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Public eventCompletedOccurs when this task has completed.
Public eventFocusDeactivatedOccurs when focus for this task is deactivated.
Public eventRevertedOccurs when this task is reverted.
Top

A Task represents a collection of input bindings and commands that can be associated with a user interface element in the designer. When a task is active, all user input is directed to the task, and commands that are associated with input bindings can be executed. When the task is deactivated, user input returns to normal.

You can adorn a task with RequiresServiceAttribute and RequiresContextItemAttribute attributes. If you do, the task will not be used unless the needed services and context items are available.

The following code example shows how to use the Task class. For more information, see How to: Create a Surrogate Policy.


using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;

using Microsoft.Windows.Design.Interaction;

namespace DemoControlLibrary.VisualStudio.Design
{
    // A DockPanelMarginTask is attached to to the adorner
    // offered by the DockPanelAdornerProvider class. When 
    // you drag the adorner, the target control's Margin
    // property changes. 
    class DockPanelMarginTask : Task 
    {
        InputBinding dragBinding, endDragBinding;
        Thickness initialMargin;

        // The DockPanelMarginTask constructor establishes mappings 
        // between user inputs and commands. 
        public DockPanelMarginTask() 
        {
            ToolCommand beginDrag = new ToolCommand("BeginDrag");
            ToolCommand drag = new ToolCommand("Drag");
            ToolCommand endDrag = new ToolCommand("EndDrag");
            ToolCommand resetMargins = new ToolCommand("ResetMargins");

            this.InputBindings.Add(
                new InputBinding(
                    beginDrag, 
                    new ToolGesture(ToolAction.DragIntent, MouseButton.Left)));

            this.InputBindings.Add(
                new InputBinding(
                    resetMargins, 
                    new ToolGesture(ToolAction.DoubleClick, MouseButton.Left)));

            this.dragBinding = new InputBinding(
                drag, 
                new ToolGesture(ToolAction.Move));

            this.endDragBinding = new InputBinding(
                endDrag, 
                new ToolGesture(ToolAction.DragComplete));

            this.ToolCommandBindings.Add(
                new ToolCommandBinding(beginDrag, OnBeginDrag));
            this.ToolCommandBindings.Add(
                new ToolCommandBinding(drag, OnDrag));
            this.ToolCommandBindings.Add(
                new ToolCommandBinding(endDrag, OnEndDrag));
            this.ToolCommandBindings.Add(
                new ToolCommandBinding(resetMargins, OnResetMargins));
        }

        private void OnBeginDrag(object sender, ExecutedToolEventArgs args) 
        {
            GestureData data = GestureData.FromEventArgs(args);

            this.BeginFocus(data);
            this.InputBindings.Add(dragBinding);
            this.InputBindings.Add(endDragBinding);

            this.initialMargin = (Thickness)data.ImpliedSource.Properties[
                "Margin"].ComputedValue;
        }

        private void OnDrag(object sender, ExecutedToolEventArgs args) 
        {
            MouseGestureData data = MouseGestureData.FromEventArgs(args);
            double offX = data.PositionDelta.X;
            double offY = data.PositionDelta.Y;

            Thickness newMargin = initialMargin;

            newMargin.Bottom += offY;
            newMargin.Top += offY;
            newMargin.Left += offX;
            newMargin.Right += offX;

            data.ImpliedSource.Properties["Margin"].SetValue(newMargin);
        }

        private void OnEndDrag(object sender, ExecutedToolEventArgs args) 
        {
            Description = "Adjust margin";
            this.Complete();
        }

        protected override void OnCompleted(EventArgs e)
        {
            this.Cleanup();
            base.OnCompleted(e);
        }

        protected override void OnReverted(EventArgs e)
        {
            this.Cleanup();
            base.OnReverted(e);
        }

        private void Cleanup()
        {
            this.InputBindings.Remove(dragBinding);
            this.InputBindings.Remove(endDragBinding);
        }

        private void OnResetMargins(object sender, ExecutedToolEventArgs args) 
        {
            GestureData data = GestureData.FromEventArgs(args);
            data.ImpliedSource.Properties["Margin"].ClearValue();
        }

    }
}


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

ADD
Show: