Move事件,拖动控件的问题,内附代码,请大家帮帮忙吧~!
用鼠标拖动容器,然后让容器上的控件按照鼠标走的值去走,用的是故事版,但是拖动快的时候,为什么会串位呢?
public Point mouseStart , mouseEnd;//记录鼠标的起始点,来计算移动位置public void MouseLeftDown( object sender , EventArgs e ) { mouseStart=Mouse.GetPosition( this ); } public void Move( object sender , EventArgs e ) { mouseEnd=Mouse.GetPosition( this ); double y=mouseEnd.Y-mouseStart.Y; //Menumain 是用户控件,grid是容器 Menumain menu=grid.Children[1] as Menumain; DB.DB.timeSpan=0; items item=null; if ( y!=0 ) { for ( int i=0 ; i<menu.sdf.Length ; i++ ) { item=menu.sdf[i] as items; if ( item.Margin.Top+y>=336 && item.Margin.Top+y<=528.5 ) { storyTapMove( item , 216 , item.Margin.Top+menuSlidePointY( item.Margin.Top , i , y ) , DB.DB.timeSpan ); DB.DB.NowIng=i+1;//这个是记录中心位置的 } else { storyTapMove( item , 240, item.Margin.Top+menuSlidePointY( item.Margin.Top , i , y ) , DB.DB.timeSpan ); } } } }#region 返回控件应移动的距离(正常值和特殊值的正常比例) /// <summary> /// 返回控件应移动的距离(正常值和特殊值的正常比例) /// </summary> /// <param name="top"></param> /// <param name="i"></param> /// <param name="y"></param> /// <returns></returns> public float menuSlidePointY( double top , int i , float y ) { float temp=y/70; if ( top>382 &&top <675 ) { return 293*temp; } else if ( top<382 && top>290 ) { return 92*temp; } return y; } #endregion#region Thickness移动主菜单动画(按margin移动) /// <summary> /// Thickness移动主菜单动画(按margin移动) /// </summary> /// <param name="item">移动控件</param> /// <param name="setLeft">控件靠左侧宽度</param> /// <param name="setTop">控件靠顶部宽度</param> public void storyTapMove( items item , double setLeft , double setTop , int timeSpan ) { Storyboard sb = new Storyboard( ); //sb.FillBehavior = FillBehavior.Stop; //item.BeginStoryboard( sb , HandoffBehavior.SnapshotAndReplace , true ); TransformGroup tfg = new TransformGroup( ); ScaleTransform sf = new ScaleTransform( ); SkewTransform qx = new SkewTransform( ); RotateTransform xz = new RotateTransform( ); TranslateTransform py = new TranslateTransform( ); tfg.Children.Add( sf ); tfg.Children.Add( qx ); tfg.Children.Add( xz ); tfg.Children.Add( py ); ThicknessAnimationUsingKeyFrames animatX=new ThicknessAnimationUsingKeyFrames( ); SplineThicknessKeyFrame sk=new SplineThicknessKeyFrame( new Thickness( setLeft , setTop , 0 , 0 ) , KeyTime.FromTimeSpan( new TimeSpan( 0 , 0 , 0 , 0 , timeSpan ) ) , new KeySpline( 0.37 , 1 , 1 , 1 ) ); animatX.KeyFrames.Add( sk ); Storyboard.SetTarget( animatX , item ); Storyboard.SetTargetProperty( animatX , new PropertyPath( "(FrameworkElement.Margin)" ) ); sb.Children.Add( animatX ); sb.Begin( ); } #endregion
到现在还是找不到好的方法解决。
[解决办法]
namespace xxxx.Controls
{
public class DragControl : UserControl
{
public delegate void DragerEventHandler(UIElement sender, EventArgs e);
public event DragerEventHandler DragerMove;
public event DragerEventHandler DragerMoveStop;
private bool isMouseDown;
public Point lastPosition;
public Point elPosition;
int zIndex = 0;
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
this.Cursor = Cursors.Hand;
isMouseDown = true;
BringToTop();
this.CaptureMouse();
lastPosition = e.GetPosition(Application.Current.RootVisual);
elPosition = new Point(Canvas.GetLeft(this), Canvas.GetTop(this));
base.OnMouseLeftButtonDown(e);
}
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
isMouseDown = false;
zIndex = 0;
this.ReleaseMouseCapture();
this.Cursor = Cursors.Arrow;
base.OnMouseLeftButtonUp(e);
if (DragerMoveStop != null) { DragerMoveStop(this, e); }
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (isMouseDown)
{
Point currentPosition = e.GetPosition(Application.Current.RootVisual);
double curX = currentPosition.X - lastPosition.X + elPosition.X;
double curY = currentPosition.Y - lastPosition.Y + elPosition.Y;
SetPosition(new Point(curX, curY));
lastPosition = currentPosition;
if (DragerMove != null)
{
DragerMove(this, e);
}
}
base.OnMouseMove(e);
}
public virtual void SetPosition(Point p)
{
Canvas.SetLeft(this, p.X);
Canvas.SetTop(this, p.Y);
elPosition = p;
//var mainCanvas = this.Parent as Canvas;
//mainCanvas.SetValue(BackgroundProperty,new SolidColorBrush(Colors.Red));
//mainCanvas.Height = 3000;
}
private double GetMaxHeight(Canvas panel)
{
double height = 0;
foreach (UIElement ele in panel.Children)
{
if (Canvas.GetTop(ele) > height)
height = Canvas.GetTop(ele);
}
return height;
}
public virtual Point GetPosition()
{
return this.elPosition;
}
public void Show()
{
this.Visibility = Visibility.Visible;
}
public void Hide()
{
this.Visibility = Visibility.Collapsed;
}
protected void BringToTop()
{
if (zIndex == 0)
{
var oldIndex = this.zIndex;
var mainCanvas = this.Parent as Canvas;
foreach (FrameworkElement fElement in mainCanvas.Children)
{
Canvas.SetZIndex(fElement, 0);
}
Canvas.SetZIndex(this, 2);
zIndex = 1;
}
}
}
}
我写的基础控件,供参考。
[解决办法]
// <summary>
/// 移动过程
/// </summary>
private void drawCanv_MouseMove(object sender, MouseEventArgs e)
{
ElementLayer el = MainMap.Layers[2] as ElementLayer;
if (gisState == GisState.Edit && editStation != null)
{
System.Windows.Point screenPoint = e.GetPosition(MainMap);
ESRI.ArcGIS.Client.Geometry.MapPoint mapPoint = MainMap.ScreenToMap(screenPoint);
ElementLayer.SetEnvelope(editStation, mapPoint.Extent);
var value = editStation;
el.Children.Remove(value);
el.Children.Add(value);
}
}