Partilhar via


Customization: Create a Shape with Collapsed Compartments

In DSL Tools, a CompartmentShape has a set of compartments in it. By default, when a CompartmentShape is placed on the Diagram, all of its compartments are expanded. There have been a few questions about how to create the shape with its compartments closed, but with the CompartmentShape itself expanded.

The following code does just that. You'll need to replace the namespace and class name with the ones in your own projects.

namespace CompanyName.ProjectName.TestLanguage.Designer
{
#region Using directives

 using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.Modeling;
using System.Diagnostics;
using Microsoft.VisualStudio.Modeling.Utilities;
using System.Drawing;
using Microsoft.VisualStudio.Modeling.Diagrams;

 #endregion

 public partial class ClassShape
{
public override void EnsureCompartments()
{
base.EnsureCompartments();

   // This only tries to force the auto-collapse if this isn't loading from
// a file. In that case, we want to stay with the state that the user saved
// rather than reset it.
if (this.TransactionManager.CurrentTransaction != null &&
this.TransactionManager.CurrentTransaction.IsSerializing == false)
{
foreach (Compartment comp in this.NestedChildShapes)
{
comp.IsExpanded = false;
}
}
}
}
}

The only wrinkle here is checking the IsSerializing flag on the transaction. This lets you know when you're in a file load operation as opposed to any other time. I added this so that the code wouldn't auto-collapse the shapes, but show the state that the user saved. If you always want to auto-collapse the compartments even when loading from a file, then just remove that check. 

It is also possible to start with the whole shape in a collapsed state. Doing this is more straight-forward, but I'm including that code as well for completeness.

namespace CompanyName.ProjectName.Language27.Designer
{
#region Using directives

 using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.Modeling;
using System.Diagnostics;
using Microsoft.VisualStudio.Modeling.Utilities;
using System.Drawing;
using Microsoft.VisualStudio.Modeling.Diagrams;

 #endregion

 public partial class ClassShape
{
public override void OnInitialized()
{
base.OnInitialized();
this.IsExpanded = false;
}
}
}

Comments