views:

49

answers:

1

[EDIT]

Alright,

I edited this post since the code I posted back then had no real links with what I'm trying to do now, but the question is the same.

When I'm talking about limiting objects to a Canvas it was more like a Mouse Clipping, but as I read on many threads, this feature doesn't exist in SL. So I searched a bit around all forums and got this link. But I wasn't able to reproduce it all. Here is the code that is used for the Drag&Drops Events:

public class RoomImage : ContentControl
{
    public RoomImage()
    {
        DefaultStyleKey = typeof(RoomImage);
    }

    public static readonly DependencyProperty BackgroundImageProperty = DependencyProperty.Register("Source", typeof(ImageSource), typeof(RoomImage), null);
    public ImageSource BackgroundImage
    {
        get { return (ImageSource)GetValue(BackgroundImageProperty); }
        set { SetValue(BackgroundImageProperty, value); }
    }

    //Instance Drag variable

    private FrameworkElement _translateZone;
    bool _isDrag;
    Point StartingDragPoint;

    public double Top
    {
        get { return (double)GetValue(Canvas.TopProperty); }
        set { SetValue(Canvas.TopProperty, value); }
    }

    public double Left
    {
        get { return (double)GetValue(Canvas.LeftProperty); }
        set { SetValue(Canvas.LeftProperty, value); }
    }

    //Instance Drag events
    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        _translateZone = GetTemplateChild("PART_TranslateZone") as FrameworkElement;
        DefineDragEvents();
    }

    private void DefineDragEvents()
    {
        if (_translateZone != null)
        {
            _translateZone.MouseLeftButtonDown += new MouseButtonEventHandler(translateZone_MouseLeftButtonDown);
            _translateZone.MouseLeftButtonUp += new MouseButtonEventHandler(translateZone_MouseLeftButtonUp);
            _translateZone.MouseMove += new MouseEventHandler(translateZone_MouseMove);
        }
    }

    private void translateZone_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        _isDrag = true;

        //start the drag
        FrameworkElement DragBar = (FrameworkElement)sender;
        DragBar.CaptureMouse();

        // drag starting point
        StartingDragPoint = e.GetPosition(this);
    }

    private void translateZone_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        FrameworkElement translateZone = (FrameworkElement)sender;
        translateZone.ReleaseMouseCapture();

        _isDrag = false;
    }

    private void translateZone_MouseMove(object sender, MouseEventArgs e)
    {
        if (_isDrag)
        {
            UIElement ui = (UIElement)this.Parent;
            Point Point = e.GetPosition(ui);

            Move(Point.X - StartingDragPoint.X, Point.Y - StartingDragPoint.Y);
        }
    }

    public void Move(double left, double top)
    {
        Left = left;
        Top = top;
    }

}

I found this part of code in a tutorial where they didn't explain the Mouse.Clip at all. I can understand it and reuse it, but I have no clue where I could set the limits. The Parent of this item is a Canvas by the way.

If anyone can provide me some sort of code, or where I should implement mine it would be great!

Thank you, Ephismen.

+1  A: 

A Canvas has no size as far as its children are concerned. It is just a relative starting point for rendering. A fixed canvas size is only relevant to the parent of the Canvas.

If you mean the objects are being drawn outside the canvas rectangle, then that is the correct behaviour for a canvas.

To stop objects being drawn outside a canvas you need to set a clipping rectangle in the Clip property of the Canvas.

Update:

Here is a very nice example here of how to have a ClipToBounds attached property. That is definitely the easiest way to implement bounds clipping I have seen.

Another update:

So you want to just keep the child items within the parent canvas. If your items vary in size & shape that is basically collision testing with the sides and cap the min/max left/top values. How complex are the shapes you are dropping? Rectangles are obviously very easy to calculate (as are circles).

Enough already
Yes you are right I was talking about object being drawn outside the canvas rectangle. I remember trying to find out how to add clipping rectangle a few times ago but failed into finding working information for Silverlight 4. Could you give me an example in code-behind(c#) please?
Ephismen
See update above. An *attached property* is a very elegant way to add bounds clipping to a rectangular canvas.
Enough already
Thank you for the link you posted, I read it but is it not really what I was searching for. What I am trying to do is not stop my item being drawn outside the canvas, it's to stop my item to even move out of the canvas. For example: I move my window to the top or left of my canvas and it should stop as if it was hitting a wall. The same for the other sides.
Ephismen
See update. You basically want to do hit-testing and cap the position values.
Enough already
Updated my own post for more information.
Ephismen