开发者

How to limit item position into Canvas?

开发者 https://www.devze.com 2023-01-16 17:17 出处:网络
[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.

[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);
          开发者_StackOverflow社区  _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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消