pmf
pmf

Reputation: 7749

WPF: TreeView height

I'm using a TreeView in WPF that has an explicit height set. The vertical scrollbar is cut off at the bottom (independent of whether items are bound/shown or not; I've enabled the scrollbar to be able to reproduce the problem without items attached). XAML source below.

<Window x:Class="demo.TreeViewProblem"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	Title="..."
    ResizeMode="CanResizeWithGrip"
	SizeToContent="WidthAndHeight">
	
	<Grid>
		<Grid.ColumnDefinitions>
			<ColumnDefinition Width="150"/>
			<ColumnDefinition Width="*"/>
		</Grid.ColumnDefinitions>

		<Grid.RowDefinitions>
			<RowDefinition />
			<RowDefinition />
			<RowDefinition />
			<RowDefinition />
			<RowDefinition />
		</Grid.RowDefinitions>

		<TextBlock
			Grid.Column="0"
			Grid.Row="0"
			Margin="5,10,5,5"
			Text="Some label"/>

		<TextBlock
			Grid.Column="1"
			Grid.Row="0"
			Margin="5,10,5,5"
			Text="Some value"/>

		<TreeView
			ScrollViewer.VerticalScrollBarVisibility="Visible"
			Grid.Column="1"
			Grid.Row="1"
			Height="200"
			MinHeight="200"
			Width="300"
			Margin="5,10,5,5"
			VerticalAlignment="Top" />

		<TextBlock
			Grid.Column="0"
			Grid.Row="2"
			Margin="5,10,5,5"
			Text="Some label"/>

		<TextBox
			Grid.Column="1"
			Grid.Row="2"
			Width="80"
			Margin="5,10,5,5"
			HorizontalAlignment="Left"
			Text="Some value"/>
		
		<TextBlock
			Grid.Column="0"
			Grid.Row="3"
			Margin="5,10,5,5"
			Text="Some label"/>

		<TextBox
			Grid.Column="1"
			Grid.Row="3"
			Width="80"
			Margin="5,10,5,5"
			HorizontalAlignment="Left"
			Text="Some value"/>
		
		<Button
			HorizontalAlignment="Center"
			Margin="10,10,10,10"
			Content="Close"
			Grid.ColumnSpan="2"
			Grid.Column="0"
			Grid.Row="4"/>

	</Grid>
</Window>

Upvotes: 0

Views: 4064

Answers (1)

RogerN
RogerN

Reputation: 3821

Change the RowDefinition for the row containing your TreeView to this:

<RowDefinition Height="Auto" />

When set to "Auto", the row will calculate how much space it needs.

EDIT: Alternatively, you could set the other rows to Auto and then make your TreeView stretch instead. With this approach your window will scale nicer when the user resizes it.

From the row definitions:

<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />

From the tree:

<TreeView VerticalAlignment="Stretch"
    ... />

Upvotes: 2

Related Questions