How to get a ListView with expandable controls to resize

I created a ListView and used an expandable control for each of the ListView’s items. Everything was fine until I noticed that after collapsing an item the ListView did not resize. Resizing worked fine in the other direction, i.e. the Listview would grow in size when an item was expanded. I thought that I was going to have to do all sorts of jiggery pokery with MeasureOverride or the such like but actually the solution was really simple.

Just set the ItemsPanel to a type that automatically resizes:

<ListView.ItemsPanel>
  <ItemsPanelTemplate>
    <StackPanel/>
  </ItemsPanelTemplate>
</ListView.ItemsPanel>

So here is my ListView in its entirety:

<ListView x:Name="lvMenus" AllowDrop="True" ItemContainerStyle="{DynamicResource ItemContStyle}"
    		HorizontalAlignment="Stretch">
	<ListView.ItemsPanel>
		<ItemsPanelTemplate>
			<StackPanel/>
		</ItemsPanelTemplate>
	</ListView.ItemsPanel>
	<ListView.View>
		<GridView>
			<GridViewColumn Header="Titles">
				<GridViewColumn.CellTemplate>
					<DataTemplate>
						<spEtr:Inspector
					          AssetId="{Binding Id, Mode=OneWay}"
						  LongTitle="{Binding LongTitle, Mode=TwoWay}"
						  LongTitleChanged="Inspector_LongTitleChanged"
						  LongTitleLostFocus="Inspector_LongTitleLostFocus"
						  LongTitleGotFocus="Inspector_LongTitleGotFocus"
						  ShortTitle="{Binding ShortenedTitle, Mode=TwoWay}"
						  ShortTitleChanged="Inspector_ShortTitleChanged"
						  ShortTitleFocused="Inspector_ShortTitleFocused"
						  ShortTitleLostFocus="Inspector_ShortTitleLostFocus"
						  ExpanderClicked="Inspector_ExpanderClicked"
						  LongTitleErrorStatusChanged="Inspector_LongTitleErrorStatusChanged"
							/>
					</DataTemplate>
				</GridViewColumn.CellTemplate>
			</GridViewColumn>
		</GridView>
	</ListView.View>
</ListView>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s