How to Change Size in Collapsed RadDiagramContainerShape
Environment
Product Version | 2019.2.510 |
Product | RadDiagram for WPF |
Description
How to preserve the collapsed size of RadDiagramContainerShape.
Solution
To preserve the collapsed size of a diagram container shape, cache the size when the resizing is finished and if the container is collapsed. And then restore it back when the container get collapsed again.
Use the CompleteResizing event of the RadDiagram's ResizingService to store the collapsed size. And the IsCollapsedChanged event of RadDiagramContainerShape to restore the size.
<telerik:RadDiagram x:Name="diagram">
<telerik:RadDiagramContainerShape Position="300, 300" Width="300" Height="100" IsCollapsible="true"
IsCollapsedChanged="RadDiagramContainerShape_IsCollapsedChanged"/>
</telerik:RadDiagram>
public partial class MainWindow : Window
{
private Dictionary<RadDiagramContainerShape, double> containerToHeightCache = new Dictionary<RadDiagramContainerShape, double>();
public MainWindow()
{
InitializeComponent();
var service = this.diagram.ServiceLocator.GetService<IResizingService>() as ResizingService;
service.CompleteResizing += Service_CompleteResizing;
}
private void Service_CompleteResizing(object sender, ResizingEventArgs e)
{
if (e.Items.Count() == 1 && e.Items.ElementAt(0) is RadDiagramContainerShape)
{
var container = (RadDiagramContainerShape)e.Items.ElementAt(0);
if (container.IsCollapsed)
{
if (!containerToHeightCache.ContainsKey(container))
{
containerToHeightCache.Add(container, e.NewBounds.Height);
}
else
{
containerToHeightCache[container] = e.NewBounds.Height;
}
}
}
}
private void RadDiagramContainerShape_IsCollapsedChanged(object sender, RoutedEventArgs e)
{
var container = (RadDiagramContainerShape)sender;
if (container.IsCollapsed)
{
container.IsResizingEnabled = true;
if (containerToHeightCache.ContainsKey(container))
{
container.Height = containerToHeightCache[container];
}
}
}
}