以下のサンプルコードでは、StackPanelコントロールが移動中のオブジェクトをテストして、他のオブジェクトの特定の距離内にあるかどうかを確認し、そのオブジェクトをシフトして他のオブジェクトに当接させるようにします。以下のコードを簡単にテストすると、あるオブジェクトを別のオブジェクトに重なるようにドラッグすることはできず、別のオブジェクトに隣接しないようにオブジェクトをドラッグすることはできません。ユーザーが1つのオブジェクトを別のオブジェクトの端にスナップする機能をWPFライブラリによって、より具体的にはStackPanelコントロールによって提供します。
OPの質問の一般的な本質に鑑みて、WPFはコントロールと機能の堅牢なライブラリを提供し、開発者があるコントロールの近接をテストして自由にオブジェクトを配置できることを指摘しますGrid、Canvas、StackPanelなどの特殊なコンテナを使用します。
MainWindow.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private bool _isDown;
private bool _isDragging;
private Point _startPoint;
private UIElement _realDragSource;
private UIElement _dummyDragSource = new UIElement();
private void sp_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.Source != this.sp)
{
_isDown = true;
_startPoint = e.GetPosition(this.sp);
}
}
private void sp_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
_isDown = false;
_isDragging = false;
_realDragSource.ReleaseMouseCapture();
}
private void sp_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (_isDown)
{
if ((! _isDragging) && ((Math.Abs(e.GetPosition(this.sp).X - _startPoint.X) > SystemParameters.MinimumHorizontalDragDistance) ||
(Math.Abs(e.GetPosition(this.sp).Y - _startPoint.Y) > SystemParameters.MinimumVerticalDragDistance)))
{
_isDragging = true;
_realDragSource = e.Source as UIElement;
_realDragSource.CaptureMouse();
DragDrop.DoDragDrop(_dummyDragSource, new DataObject("UIElement", e.Source, true), DragDropEffects.Move);
}
}
}
private void sp_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent("UIElement"))
e.Effects = DragDropEffects.Move;
}
private void sp_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent("UIElement"))
{
UIElement droptarget = e.Source as UIElement;
int droptargetIndex = -1, i = 0;
foreach (UIElement element in this.sp.Children)
{
if (element.Equals(droptarget))
{
droptargetIndex = i;
break;
}
i++;
}
if (droptargetIndex != -1)
{
this.sp.Children.Remove(_realDragSource);
this.sp.Children.Insert(droptargetIndex, _realDragSource);
}
_isDown = false;
_isDragging = false;
_realDragSource.ReleaseMouseCapture();
}
}
}
MainWindow.xaml
<Window x:Class="DragShapes.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">
<Border Background="Red">
<StackPanel Name="sp" AllowDrop="True" Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center"
PreviewMouseLeftButtonDown="sp_PreviewMouseLeftButtonDown"
PreviewMouseLeftButtonUp="sp_PreviewMouseLeftButtonUp"
PreviewMouseMove="sp_PreviewMouseMove"
DragEnter="sp_DragEnter" Drop="sp_Drop">
<Rectangle Height="80" Width="100" Fill="SkyBlue" />
<Rectangle Height="110" Width="90" Fill="LightGreen" />
<Rectangle Height="60" Width="60" Fill="Violet" />
</StackPanel>
</Border>
</Window>
出典
2016-06-26 00:17:20
Sam
あなたはXAML/WPF/UWPを調査したことがありますか? – Sam
簡単に - 私は戻ってきて、彼らをよく見ていきます、ありがとう! – DSGD
私はほとんど答えを "比較的簡単に"と答えています。あるいは、あなたが使用しているグラフィカルなAPIであれば何か似たようなことをしなければならないので、それが "比較的簡単"だと思えば、どのAPIでも比較的簡単です。 JavaScript言語はこれを「すぐに」提供するものではなく、Winforms、WPF、Winrt、XamarinなどのC#で一般的に使用されるグラフィカルAPIもありません。グラフィカルなAPIを理解したら、すでに見つかったJavaScriptコードの基本的なテクニックを適用できるはずです。 –