One property more

Jun 2, 2009 at 12:25 AM

I'm not good writing english.

This extensions it's very good, but I needed a new property to prevent it from leaving the container object.

I modify the method OnDragging of the class DragBehaviour, to this:

<!-- p.MsoNormal {margin-bottom:.0001pt; font-size:12.0pt; font-family:"Times New Roman"; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; } .style1 { margin-left: 40px; } .style2 { margin-left: 80px; } -->

private static void OnDragging( object sender, System.Windows.Input.MouseEventArgs e )

{

var obj = sender as DependencyObject;

if ( !GetIsDragging( obj ) )

      return;

 

Point pos = e.GetPosition( obj as IInputElement );

      double horizontalChange = pos.X - GetOriginalX( obj );

      double verticalChange = pos.Y - GetOriginalY( obj );

 

      if ( double.IsNaN( GetX( obj ) ) )

      SetX(obj, 0);

 if ( double.IsNaN( GetY( obj ) ) )

      SetY( obj, 0 );

                   

      //read the property

      bool onlyIn = GetOnlyIn(obj);

 

      double wContainer = 0;

      double hContainer = 0;

      double maxX = 0;

      double maxY = 0;

      if (onlyIn && (sender as FrameworkElement) != null && ((sender as FrameworkElement).Parent as FrameworkElement) != null)

      {

      wContainer = ((sender as FrameworkElement).Parent as FrameworkElement).ActualWidth;

            hContainer = ((sender as FrameworkElement).Parent as FrameworkElement).ActualHeight;

            maxX = wContainer - (sender as FrameworkElement).ActualWidth;

            maxY = hContainer - (sender as FrameworkElement).ActualHeight;

}

 

if (double.IsNaN(GetX(obj)))

      SetX(obj, 0);

if (double.IsNaN(GetY(obj)))

SetY(obj, 0);

 

double newPosX = GetX(obj) + horizontalChange;

      double newPosY = GetY(obj) + verticalChange;

      newPosX = (onlyIn && (newPosX < 0 || newPosX > maxX)) ? newPosX < 0 ? 0 : maxX : newPosX;

      newPosY = (onlyIn && (newPosY < 0 || newPosY > maxY)) ? newPosY < 0 ? 0 : maxY : newPosY;

 

            //move the object

SetX(obj, newPosX);

      SetY(obj, newPosY);

 

      e.Handled = true

}

And the new property:

public static readonly DependencyProperty OnlyInProperty = DependencyProperty.RegisterAttached("OnlyIn", typeof(bool), typeof(DragBehaviour), new UIPropertyMetadata(false));

 

public static bool GetOnlyIn(DependencyObject obj)

{

return (bool)obj.GetValue(OnlyInProperty);

}

 

public static void SetOnlyIn(DependencyObject obj, bool value)

{

obj.SetValue(OnlyInProperty, value);

}

 

And finally for testing, in the style XAML:

 

<Setter Property="extbehaviour:DragBehaviour.OnlyIn" Value="True" />

 

I changed this code for get out from my problem quickly, and I wanted to comment for consideration in your project. 

 

Greetings!

 

Coordinator
Jun 2, 2009 at 9:22 AM

Thanks, that's a great improvement, i will definitely add this to the source code...

Thanks again!

Aug 30, 2010 at 4:20 PM

I don't see this property in the current source code. Is it implemented? If so, is it named "OnlyIn"?