Why is a Line shape on a Canvas drifting away while dragging?

前端 未结 2 1427
闹比i
闹比i 2021-01-27 02:14

I see a strange behaviour dragging in Line shape over a Canvas that I cannot explain. The Line shape is in a Thumb DataTemplate like so:



        
2条回答
  •  庸人自扰
    2021-01-27 02:26

    I had the same problem and after this answer "For a Line the correct value to animate the dragging of the ellipse on the canvas is the difference of the displacement between two consecutive DragDelta events." I developed this solution, worked for me, hope this helps someone

     public class Connector : Thumb
    {
        private double _oldX = 0;
        private double _oldY = 0;
    
    
        public Connector()
        {
            DragDelta += Connector_DragDelta;
            DragCompleted += Connector_DragCompleted;
        }
    
        private void Connector_DragCompleted(object sender, DragCompletedEventArgs e)
        {
            _oldX = 0;
            _oldY = 0;
        }
    
        private void Connector_DragDelta(object sender, DragDeltaEventArgs e)
        {
            Thumb thumb = e.Source as Thumb;
            DiagramConnectorViewModel viewModel = (DiagramConnectorViewModel)thumb.DataContext;
    
            double leftOffSet = _oldX - e.HorizontalChange;
            _oldX = e.HorizontalChange;
            viewModel.X1 -= leftOffSet;
            viewModel.X2 -= leftOffSet;
    
            double topOffSet = _oldY - e.VerticalChange;
            _oldY = e.VerticalChange;
            viewModel.Y1 -= topOffSet;
            viewModel.Y2 -= topOffSet;
    
        }
    

提交回复
热议问题