Skip to content

DevExpress-Examples/winforms-diagram-proportionally-resize-shapes-within-container

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WinForms DiagramControl - Proportionally Resize Shapes Within the Parent Container

This example resizes inner shapes when the associated parent container is resized. You can introduce the logic herein if you create custom shapes based on containers or use containers to group shapes.

Implementation Details

  1. Create a DiagramContainer class descendant to retain the behavior of standard containers:

    public class CustomDiagramContainer : DiagramContainer { }
  2. Handle the DiagramControl.BeforeItemsResizing event and pass container child items to the e.Items collection:

    private void DiagramControl1_BeforeItemsResizing(object sender, DiagramBeforeItemsResizingEventArgs e) {
        var containers = e.Items.OfType<CustomDiagramContainer>();
        foreach (var customContainer in containers) {
            e.Items.Remove(customContainer);
            foreach (var item in customContainer.Items)
                e.Items.Add(item);
        }
    }

    In this instance, the DiagramControl resizes these inner items instead of the parent container.

  3. Handle the DiagramControl.ItemsResizing event and correct container position and size:

    private void DiagramControl1_ItemsResizing(object sender, DiagramItemsResizingEventArgs e) {
        var groups = e.Items.GroupBy(x => x.Item.ParentItem);
        foreach (var group in groups) {
            if (group.Key is CustomDiagramContainer) {
                var customContainer = (CustomDiagramContainer)group.Key;
                var containingRect = customContainer.Items.Select(x => x.RotatedDiagramBounds().BoundedRect()).Aggregate(Rect.Empty, Rect.Union);
                customContainer.Position = new PointFloat((float)containingRect.X, (float)containingRect.Y);
                customContainer.Width = (float)containingRect.Width;
                customContainer.Height = (float)containingRect.Height;
            }
        }
    }

Files to Review

Documentation

More Examples

About

Resize inner shapes when the associated parent container is resized.

Topics

Resources

License

Stars

Watchers

Forks