Task クラス

更新 : 2007 年 11 月

コマンドのコレクション、およびこれらのコマンドへの入力バインディングを表します。

名前空間 :  Microsoft.Windows.Design.Interaction
アセンブリ :  Microsoft.Windows.Design.Interaction (Microsoft.Windows.Design.Interaction.dll 内)

構文

'宣言
Public Class Task
'使用
Dim instance As Task
public class Task
public ref class Task
public class Task

解説

Task は、デザイナのユーザー インターフェイス要素に関連付けることができる入力バインディングとコマンドのコレクションを表します。タスクがアクティブのとき、すべてのユーザー入力はタスクに送られ、入力バインディングに関連付けられたコマンドが実行可能になります。タスクが非アクティブ化されると、ユーザー入力は通常に戻ります。

RequiresServiceAttribute 属性と RequiresContextItemAttribute 属性を使用して、タスクを装飾できます。これにより、必要なサービスとコンテキスト項目が使用できるようになるまで、タスクは使用されなくなります。

Task クラスを使用する方法を次のコード例に示します。詳細については、「方法 : サロゲート ポリシーを作成する」を参照してください。

Imports System
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Input

Imports Microsoft.Windows.Design.Interaction

' 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
    Inherits Task

    Private dragBinding, endDragBinding As InputBinding
    Private initialMargin As Thickness

    ' The DockPanelMarginTask constructor establishes mappings 
    ' between user inputs and commands. 
    Public Sub New() 
        Dim beginDrag As New ToolCommand("BeginDrag")
        Dim drag As New ToolCommand("Drag")
        Dim endDrag As New ToolCommand("EndDrag")
        Dim resetMargins As New ToolCommand("ResetMargins")

        Me.InputBindings.Add(New InputBinding( _
            beginDrag, _
            New ToolGesture(ToolAction.DragIntent, MouseButton.Left)))

        Me.InputBindings.Add( _
            New InputBinding( _
                resetMargins, _
                New ToolGesture(ToolAction.DoubleClick, MouseButton.Left)))

        Me.dragBinding = New InputBinding( _
            drag, _
            New ToolGesture(ToolAction.Move))

        Me.endDragBinding = New InputBinding( _
            endDrag, _
            New ToolGesture(ToolAction.DragComplete))

        Me.ToolCommandBindings.Add(New ToolCommandBinding(beginDrag, AddressOf OnBeginDrag))
        Me.ToolCommandBindings.Add(New ToolCommandBinding(drag, AddressOf OnDrag))
        Me.ToolCommandBindings.Add(New ToolCommandBinding(endDrag, AddressOf OnEndDrag))
        Me.ToolCommandBindings.Add(New ToolCommandBinding(resetMargins, AddressOf OnResetMargins))

    End Sub

    Private Sub OnBeginDrag(ByVal sender As Object, ByVal args As ExecutedToolEventArgs) 
        Dim data As GestureData = GestureData.FromEventArgs(args)

        Me.BeginFocus(data)
        Me.InputBindings.Add(dragBinding)
        Me.InputBindings.Add(endDragBinding)

        Me.initialMargin = CType(data.ImpliedSource.Properties(FrameworkElement.MarginProperty).ComputedValue, Thickness)

    End Sub

    Private Sub OnDrag(ByVal sender As Object, ByVal args As ExecutedToolEventArgs) 
        Dim data As MouseGestureData = MouseGestureData.FromEventArgs(args)
        Dim offX As Double = data.PositionDelta.X
        Dim offY As Double = data.PositionDelta.Y

        Dim newMargin As Thickness = initialMargin

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

        data.ImpliedSource.Properties(FrameworkElement.MarginProperty).SetValue(newMargin)

    End Sub

    Private Sub OnEndDrag(ByVal sender As Object, ByVal args As ExecutedToolEventArgs) 
        Description = "Adjust margin"
        Me.Complete()

    End Sub

    Protected Overrides Sub OnCompleted(ByVal e As EventArgs) 
        Me.Cleanup()
        MyBase.OnCompleted(e)

    End Sub

    Protected Overrides Sub OnReverted(ByVal e As EventArgs) 
        Me.Cleanup()
        MyBase.OnReverted(e)

    End Sub

    Private Sub Cleanup() 
        Me.InputBindings.Remove(dragBinding)
        Me.InputBindings.Remove(endDragBinding)

    End Sub

    Private Sub OnResetMargins(ByVal sender As Object, ByVal args As ExecutedToolEventArgs) 
        Dim data As GestureData = GestureData.FromEventArgs(args)
        data.ImpliedSource.Properties(FrameworkElement.MarginProperty).ClearValue()

    End Sub
End Class
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[
                FrameworkElement.MarginProperty].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[FrameworkElement.MarginProperty].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[FrameworkElement.MarginProperty].ClearValue();
        }

    }
}

継承階層

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

スレッド セーフ

この型のすべてのパブリック static (Visual Basic では Shared) メンバは、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。

参照

参照

Task メンバ

Microsoft.Windows.Design.Interaction 名前空間

その他の技術情報

ツール アーキテクチャ

WPF デザイナの機能拡張