This is a migrated thread and some comments may be shown as answers.

RadDiagramContainerShape

2 Answers 99 Views
Diagram
This is a migrated thread and some comments may be shown as answers.
Andrew
Top achievements
Rank 1
Andrew asked on 27 Nov 2012, 07:06 PM
Can i restrict the children of a RadDiagramContainerShape so they cant leave it?

2 Answers, 1 is accepted

Sort by
0
Francois
Telerik team
answered on 28 Nov 2012, 12:04 PM
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.

0
Matt
Top achievements
Rank 1
answered on 05 Mar 2016, 06:21 PM
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.  
Tags
Diagram
Asked by
Andrew
Top achievements
Rank 1
Answers by
Francois
Telerik team
Matt
Top achievements
Rank 1
Share this question
or