Skip to content

Latest commit

 

History

History
75 lines (56 loc) · 4.59 KB

File metadata and controls

75 lines (56 loc) · 4.59 KB

WPF 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 Point(containingRect.X, containingRect.Y);
                customContainer.Width = (float)containingRect.Width;
                customContainer.Height = (float)containingRect.Height;
            }
        }
    }

Files to Review

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)