2016-09-27 1 views
0

私は図面プロジェクトに取り組んでいます。ここでは、ユーザーができるドラッグ可能なウィンドウを作成していますオブジェクトをクリックし、私たちのレイヤーでPhotoShopのように別の位置に移動します。WFP TranslateTransformエラー

プログラム私は、画面上のオブジェクトOneに対して完全に動作しています。ドラッグするだけで、任意の位置に移動できます。しかし、オブジェクトの数が増加すると、それは非常に奇妙な問題を引き起こしています。いずれかのオブジェクトをクリックすると、ウィンドウ上のすべてのオブジェクトのクラスターが作成され、クラスター全体に変換が適用されます。

注:すべてのオブジェクトは、画像であり、コンテナがキャンバスです。

使用している画像とコードは次のとおりです。

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; 
    } 

これは単一のオブジェクトです。これは、私がどんな混乱もなく簡単に望む場所に移動できます。 enter image description here

ここには3つのオブジェクトがあります。私がそれらのいずれかをクリックすると、彼らはクラスタを作り、一緒に動くようになる。 enter image description here

答えて

1

問題はすべての要素に対して同じTranslateTransformインスタンスを使用していることです。代わりに、要素を作成するとき、または要素を作成するときに、要素ごとに新しいインスタンスを作成する必要があります。 MouseDownイベントハンドラで:

private void DropList_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
{ 
    _element = FindAnchestor<Image>((DependencyObject)e.OriginalSource); 

    if (_element != null) 
    { 
     var point = e.GetPosition((IInputElement)sender); 
     _isInDrag = true; 
     _element.CaptureMouse(); 
     _element.RenderTransform = new TranslateTransform(point.X, point.Y); // here 
     e.Handled = true; 
    } 
} 

と移動するときにそれを使用します。

private void DropList_MouseMove(object sender, MouseEventArgs e) 
{ 
    if (_isInDrag) 
    { 
     var point = e.GetPosition((IInputElement)sender); 
     var transform = (TranslateTransform)_element.RenderTransform; 
     transform.X = point.X; 
     transform.Y = point.Y; 
    } 
} 
+0

はあなたの助けのためにありがとうございました。私はすべてのオブジェクトを同じ変換オブジェクトを使用していることを完全に忘れています。 –

関連する問題