What are some different methods to drag and move objects around without using Horizontal or Vertical Alignment set to Left and Top

StackOverflow https://stackoverflow.com/questions/4339547

Question

I am developing a Dashboard application where I would like the user to be able to resize and move their chart widgets to where they please. I have had problems with the being able to move and resize at the same time. The reason why is that when the chart alignments are set to the following:

chart.HorizontalAlignment = HorizontalAlignment.Left;
chart.VerticalAlignment = VerticalAlignment.Top;

...the move will work perfectly, however the resize will go crazy in undetectable directions. If I switch the alignments to the following :

chart.HorizontalAlignment = HorizontalAlignment.Stretch;
chart.VerticalAlignment = VerticalAlignment.Stretch;

...the move will be off but the resize will work decently.

The move code is as follows :

public void chart_MouseMove(object sender, MouseEventArgs e)
{
    C1Chart chart = sender as C1Chart;

       if (!ModifierKey)
       {
           if (isMouseCaptured)
           {
               // Calculate the current position of the object.
               double deltaV = e.GetPosition(null).Y - mouseVerticalPosition;
               double deltaH = e.GetPosition(null).X - mouseHorizontalPosition;
               double newTop = deltaV + (double)chart.Margin.Top;
               double newLeft = deltaH + (double)chart.Margin.Left;

               // Set new position of object.            
               chart.Margin = new Thickness(newLeft, newTop, 0, 0); 

               // Update position global variables.
               mouseVerticalPosition = e.GetPosition(null).Y;
               mouseHorizontalPosition = e.GetPosition(null).X;
          }
      }
}

The mouseVerticalPosition, mouseHorizontalPosition, isMouseCaptured are assigned in a chart mouse down event and ModifierKey is boolean to tell if any of the keyboard modifiers are pressed to know whether or not to move. This code was taken off of MSDN, but I am open to alternate solutions.

The resize code can be found here(Microsoft Example) or here(Denis Vuyka Blog). Here is my question : Is there different way to do the drag & move or the resizing (Adorner Class) that I am not seeing. Or, is there a way to use this code with an alignment hack.

Was it helpful?

Solution

Have you looked into the MouseDragElementBehavior? http://blogs.msdn.com/b/dphill/archive/2009/09/25/blend-behaviors.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top