I wanted a treeviews max height to be about 15% smaller than the window hosting it. This was because I wasn’t able to get (could not get) the vertical scrollbar for the treeview to show up without setting the MaxHeight property.
Statically setting the MaxHeight property did make the vertical scrollbar show up, however when I maximized the window, the treeview container did not maximize within the window, so it didn’t look good. This was even with the verticalAlignment and HorizontalAlignment properties set to stretch and having row and column definitions correctly configured.
So my requirement was to set the treeview control MaxHeight property value relative to the size of the window it was hosted in. We also need the scrollbar to show up when the MaxHeight is exceeded.
Within the XAML window tag I added the event.
I then added the event in the code-behind to set the MaxHeight for the treeview relative to the ActualHeight of the window. I used a ratio of 85%. The treeview will be 85% the height of the window.
private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
double actualHieght = NameOfTheWindow.ActualHeight;
treeView1.MaxHeight = actualHieght * .85;
The requirements have been met. The treeview grows in size relative to the window and the vertical scrollbar is present with the contents of the treeview exceed the MaxHeight.
NOTE: that the event fires once when the window is loading and then again once the size is changed after load. Sometimes you read SizeChanged event fires twice when searching for a solution to this problem. However, in the above solution, the fact that it fires twice doesn’t cause any problems and I therefore took no action.
Download the source