Architect's Log

I'm a Cloud Architect. I'm highly motivated to reduce toils with driving DevOps.

ドラッグで移動可能な領域を実装する

アプリ実行

起動


ドラッグ

ソースコード

App.xaml
<Application x:Class="HelloWorld.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
</Application>
MainWindow.xaml
<Window x:Class="HelloWorld.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Canvas>
        <Thumb
            Canvas.Top ="5"
            Canvas.Left="5"
            Width="30"
            Height="30"
            Name="thmub" />
    </Canvas>
</Window>
MainWindow.xaml.cs
using System.Windows;
using System.Windows.Controls;

namespace HelloWorld {
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();

            ControlPosition currentPosition = null;

            this.thmub.DragStarted += (s, e) => {
                currentPosition = new ControlPosition(
                    Canvas.GetLeft(this.thmub), Canvas.GetTop(this.thmub));
            };

            this.thmub.DragDelta += (s, e) => {
                currentPosition.Move(e.HorizontalChange, e.VerticalChange);
                Canvas.SetLeft(this.thmub, currentPosition.Left);
                Canvas.SetTop(this.thmub, currentPosition.Top);
            };
        }

        private class ControlPosition {
            public double Left { get; set; }
            public double Top { get; set; }

            public ControlPosition(double left, double top) {
                this.Left = left;
                this.Top = top;
            }

            public void Move(double left, double top) {
                this.Left += left;
                this.Top += top;
            }
        }

    }
}