Hello,
I have some problems with templated nodes and resizing.
The content of my nodes is created dynamically. The user even has the possiblity to show/hide some contents, which are displayed inside the nodes. So it is not possible to set a fixed size for the nodes.
So I created a class, which derives from TemplatedNode.
And I added a method "ResizeToFit" to this class.
public void ResizeToFit()
{
if (GetVisualChild(0) is UIElement child)
{
child.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
SetBounds(new Rect(Bounds.Left, Bounds.Top, child.DesiredSize.Width, child.DesiredSize.Height), true, true);
}
}
At first I'm adding all the nodes and the connections to the Diagram.
Afterwards I call my ResizeToFit method to set the correct size on all of the nodes.
And in the end I'm using a LayeredLayout to position the nodes.
This has to be done in this order, otherwise the layout wouldn't work correctly as the right size of the nodes wasn't calculated before.
In most of the cases this solution works well. But there are some cases where a long text is displayed in the node. So I've set a MaxWidth that the node doesn't get too big. So I had to enable TextWrapping for the TextBlock, which displays this long text.
And this is where the problem starts. As long as the text only needs one line everything is alright. But the height of the nodes isn't calculated correctly, when the next spans over multiple lines.
I think this problem occurs, because the TextBlock isn't rendered when the calculation of the size is done. So it is treated as a TextBlock with only one line.
When I override the OnRender method and put the resizing in this method the size is calculated correctly, but this is causing several other problems.
So is there any easy solution, which could be used to achive the auto-sizing of a node?