【发布时间】:2016-09-27 05:22:24
【问题描述】:
我正在做一个绘图项目。我没有在其中创建一个可拖动的窗口,用户可以在其中单击任何对象并将其移动到不同的位置,就像我们在 PhotoShop 中使用我们的图层所做的那样。
我编写的程序非常适合屏幕上的 One 对象,我可以通过简单地拖动它来移动到我想要的任何位置。但是当对象的数量增加时,就会导致一个非常奇怪的问题。当我单击任何对象时,它会在窗口上创建一个包含所有对象的集群,并开始将转换应用于整个集群。
注意:所有对象都是Images,Container是Canvas。
这是我正在使用的一些图像和代码。
private Point _currentPoint;
private Point _ancherPoint;
private bool _isInDrag = false;
private TranslateTransform _transform = new TranslateTransform();
private Image _element;
private void DropList_MouseLeftButtonDown( object sender, MouseButtonEventArgs e )
{
_element = FindAnchestor<Image>( ( DependencyObject )e.OriginalSource );
if( _element != null )
{
_ancherPoint = e.GetPosition( DropList );
_isInDrag = true;
_element.CaptureMouse();
e.Handled = true;
}
}
private void DropList_MouseMove( object sender, MouseEventArgs e )
{
if( _isInDrag )
{
_currentPoint = e.GetPosition( DropList );
_transform.X += ( _currentPoint.X - _ancherPoint.X );
_transform.Y += ( _currentPoint.Y - _ancherPoint.Y );
Lbl.Content = _element.Source.ToString();
Source on which transfrom is going to happen
_element.RenderTransform = _transform;
_ancherPoint = _currentPoint;
}
}
private void DropList_MouseLeftButtonUp( object sender, MouseButtonEventArgs e )
{
if( _isInDrag )
{
_element.ReleaseMouseCapture();
_isInDrag = false;
e.Handled = true;
}
}
private static T FindAnchestor<T>( DependencyObject current ) where T : DependencyObject
{
do
{
if( current is T )
{
return ( T )current;
}
current = VisualTreeHelper.GetParent( current );
}
while( current != null );
return null;
}
【问题讨论】:
标签: wpf drag-and-drop wpf-controls draggable mousemove