MatthewHagemann
MatthewHagemann

Reputation: 1195

Resetting a Stack Panel height to dynamic

If no height is set, a Stack Panel's height is dynamic based on its contents. My question is: if the c# code sets the height, is there a way (in c#) to wipe those values out and return the Stack Panel to sizing dynamically? Something like this...

spnTest.Height = 100; //expands height to 100, even if contents don't fill it
spnTest.HeightIsDynamic = true; //allows the height of spnTest to go back to being dynamic and expand/contract based on its contents

For some reason, setting the height to double.NaN has no effect...

stkTest.Height = 117;
Console.WriteLine($"Height = {stkTest.Height}"); //writes "Height = 117"
stkTest.Height = double.NaN;
Console.WriteLine($"Height = {stkTest.Height}"); //writes "Height = 117"

.ClearValue(HeightProperty) also does not work

Upvotes: 1

Views: 179

Answers (1)

Andrew Ebert
Andrew Ebert

Reputation: 62

A StackPanel initially measures the needed size of the element by using the sizes of the child elements contained within. This initial measurement should be stored in DesiredSize. I would try using the height that is stored there to get the original measurement for the Element. Check out MSDN entry.

MSDN StackPanel

Upvotes: 1

Related Questions