RadDiagramContainerShape

3 posts, 0 answers
  1. Andrew
    Andrew avatar
    18 posts
    Member since:
    Sep 2012

    Posted 27 Nov 2012 Link to this post

    Can i restrict the children of a RadDiagramContainerShape so they cant leave it?
  2. Francois
    Admin
    Francois avatar
    10 posts

    Posted 28 Nov 2012 Link to this post

    Andrew,

    it all depends on what you want. You will find a working solution below but it doesn't answer what happens when the user resizes a shape, how will a shape initially get into the container and so on. A solution embracing all this (and potentially other business related constraints) would demand a custom container based on the RadDiagramShapeBase class and would in essence be a completely different thing than the default container we shipped. In addition, the selection and resizing adorner is currently not OK and unless that code is customized as well you need to disable the manipulation adorner. In any case, have a look at the code and see if this helps you to move onwards to what you aim for.

     

    public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                var container = new ConstrainedContainerShape
                {
                    Position = new Point(120, 250),
                    ContainerLayout = ContainerLayout.Fixed,
                    Width = 400,
                    Height = 280
                };
                this.diagram.AddShape(container);
     
                var shape = new ConstrainedShape
                {
                    Geometry = ShapeFactory.GetShapeGeometry(CommonShapeType.RectangleShape),
                    Background = Brushes.DarkOrange,
                    Position = new Point(130, 260)
                };
                container.Items.Add(shape);
                shape.ParentContainer = container;
            }
     
              
        }
     
        public class ConstrainedContainerShape : RadDiagramContainerShape
        {
            protected override void OnPositionChanged(Point oldPosition, Point newPosition)
            {
                base.OnPositionChanged(oldPosition, newPosition);
                var offset = newPosition - oldPosition;
                foreach (var child in this.Children.OfType<ConstrainedShape>().ToArray()) child.Offset(offset);
            }
        }
     
        public class ConstrainedShape : RadDiagramShape
        {
            public RadDiagramContainerShape ParentContainer { get; set; }
            public ConstrainedShape()
            {
                this.IsManipulationAdornerVisible = false;
            }
            public void Offset(Vector offset)
            {
                base.OnPositionChanged(this.Position, this.Position + offset);
            }
            protected override void OnPositionChanged(Point oldPosition, Point newPosition)
            {
                 
                if (this.ParentContainer != null)
                {
                    var innnerRect = new Rect(this.ParentContainer.ActualBounds.TopLeft, new Point(this.ParentContainer.ActualBounds.BottomRight.X - this.ActualBounds.Width, this.ParentContainer.ActualBounds.BottomRight.Y - this.ActualBounds.Height));
                    if (innnerRect.Contains(newPosition)) base.OnPositionChanged(oldPosition, newPosition);
                    else this.Position = oldPosition;
                }
                else
                {
                    base.OnPositionChanged(oldPosition, newPosition);
                }
            }
        }
    Hope this helps,
    Francois
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft banner
  4. Matt
    Matt avatar
    5 posts
    Member since:
    Feb 2016

    Posted 05 Mar Link to this post

    If you are ok with the Shapes inside not being able to be moved, then you can just set the isDraggable to false on the shapes inside the Container and that took care of it for me.  
Back to Top