Skip to content

Commit

Permalink
dragDropObjects: Move canvas and UI children into xaml. (microsoft#503)
Browse files Browse the repository at this point in the history
  • Loading branch information
drusteeby-AC authored Dec 15, 2023
1 parent d2f3fff commit 9dc9b2d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 42 deletions.
49 changes: 9 additions & 40 deletions Drag and Drop/DragDropObjects/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace DragDropObjects
{
Expand All @@ -18,41 +16,14 @@ public partial class MainWindow : Window
{
private bool _isDown;
private bool _isDragging;
private Canvas _myCanvas;
private UIElement _originalElement;
private double _originalLeft;
private double _originalTop;
private SimpleCircleAdorner _overlayElement;
private Point _startPoint;

public void OnPageLoad(object sender, RoutedEventArgs e)
{
_myCanvas = new Canvas();

var rect1 = new Rectangle();
rect1.Height = rect1.Width = 32;
rect1.Fill = Brushes.Blue;

Canvas.SetTop(rect1, 8);
Canvas.SetLeft(rect1, 8);


var tb = new TextBox {Text = "This is a TextBox. Drag and drop me"};
Canvas.SetTop(tb, 100);
Canvas.SetLeft(tb, 100);

_myCanvas.Children.Add(rect1);
_myCanvas.Children.Add(tb);

_myCanvas.PreviewMouseLeftButtonDown += MyCanvas_PreviewMouseLeftButtonDown;
_myCanvas.PreviewMouseMove += MyCanvas_PreviewMouseMove;
_myCanvas.PreviewMouseLeftButtonUp += MyCanvas_PreviewMouseLeftButtonUp;
PreviewKeyDown += window1_PreviewKeyDown;

myStackPanel.Children.Add(_myCanvas);
}

private void window1_PreviewKeyDown(object sender, KeyEventArgs e)
private void Window1_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape && _isDragging)
{
Expand All @@ -64,12 +35,12 @@ private void MyCanvas_PreviewMouseLeftButtonUp(object sender, MouseButtonEventAr
{
if (_isDown)
{
DragFinished(false);
DragFinished();
e.Handled = true;
}
}

private void DragFinished(bool cancelled)
private void DragFinished(bool cancelled = false)
{
Mouse.Capture(null);
if (_isDragging)
Expand All @@ -92,9 +63,9 @@ private void MyCanvas_PreviewMouseMove(object sender, MouseEventArgs e)
if (_isDown)
{
if ((_isDragging == false) &&
((Math.Abs(e.GetPosition(_myCanvas).X - _startPoint.X) >
((Math.Abs(e.GetPosition(MyCanvas).X - _startPoint.X) >
SystemParameters.MinimumHorizontalDragDistance) ||
(Math.Abs(e.GetPosition(_myCanvas).Y - _startPoint.Y) >
(Math.Abs(e.GetPosition(MyCanvas).Y - _startPoint.Y) >
SystemParameters.MinimumVerticalDragDistance)))
{
DragStarted();
Expand All @@ -119,27 +90,25 @@ private void DragStarted()

private void DragMoved()
{
var currentPosition = Mouse.GetPosition(_myCanvas);
var currentPosition = Mouse.GetPosition(MyCanvas);

_overlayElement.LeftOffset = currentPosition.X - _startPoint.X;
_overlayElement.TopOffset = currentPosition.Y - _startPoint.Y;
}

private void MyCanvas_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.Source == _myCanvas)
if (e.Source == MyCanvas)
{
}
else
{
_isDown = true;
_startPoint = e.GetPosition(_myCanvas);
_startPoint = e.GetPosition(MyCanvas);
_originalElement = e.Source as UIElement;
_myCanvas.CaptureMouse();
MyCanvas.CaptureMouse();
e.Handled = true;
}
}
}

// Adorners must subclass the abstract base class Adorner.
}
11 changes: 9 additions & 2 deletions Drag and Drop/DragDropObjects/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DragDropObjects"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Loaded="OnPageLoad">
Title="MainWindow" Height="350" Width="525" PreviewKeyDown="Window1_PreviewKeyDown">
<StackPanel Name="myStackPanel">
<Border Background="#99FFFFFF" BorderBrush="#CCCCFF" BorderThickness="2" Padding="20">
<TextBlock Width="750" TextWrapping="Wrap" FontSize="12">
<TextBlock TextWrapping="Wrap" FontSize="12">
This sample shows how to drag and drop objects on the screen.
To visually indicate that the object is being dragged, a simple adorner
is applied to an object as you drag it. Drag and drop the box and
TextBox below.
</TextBlock>
</Border>
<Canvas Name="MyCanvas"
PreviewMouseLeftButtonDown="MyCanvas_PreviewMouseLeftButtonDown"
PreviewMouseMove="MyCanvas_PreviewMouseMove"
PreviewMouseLeftButtonUp="MyCanvas_PreviewMouseLeftButtonUp">
<Rectangle Fill="Blue" Height="32" Width="32" Canvas.Top="8" Canvas.Left="8"/>
<TextBox Text="This is a TextBox. Drag and drop me" Canvas.Top="100" Canvas.Left="100"/>
</Canvas>
</StackPanel>
</Window>

0 comments on commit 9dc9b2d

Please sign in to comment.