I have a WPF/Silverlight ListView whose height is unpredictable and too high. How do I control it be
- by Rob Perkins
I have a ListView element with a DataTemplate for each ListViewItem defined as follows. When run, the ListView's height is not collapsed onto the items in the view, which is undesirable behavior:
<DataTemplate x:Key="LicenseItemTemplate">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding company}"></TextBlock>
<Grid Grid.Row="1" Style="{StaticResource HiddenWhenNotSelectedStyle}">
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<Button Grid.Row="0">ClickIt</Button>
</Grid>
</Grid>
</DataTemplate>
The second row of the outer grid has a style applied which looks like this. The purpose of the style is to :
<Style TargetType="{x:Type Grid}" x:Key="HiddenWhenNotSelectedStyle" >
<Style.Triggers>
<DataTrigger
Binding="{Binding Path=IsSelected,
RelativeSource={
RelativeSource
Mode=FindAncestor,
AncestorType={x:Type ListViewItem}
}
}"
Value="False">
<Setter Property="Grid.Visibility" Value="Collapsed" />
</DataTrigger>
<DataTrigger
Binding="{Binding Path=IsSelected,
RelativeSource={
RelativeSource
Mode=FindAncestor,
AncestorType={x:Type ListViewItem}
}
}"
Value="True">
<Setter
Property="Grid.Visibility"
Value="Visible"
/>
</DataTrigger>
</Style.Triggers>
</Style>
The ListView renders like this:
The desired appearance is this, when none of the elements are selected:
...with, of course, the ListView's height adjusting to accommodate the additional content when the second grid is made visible by selection. What can I do to get the desired behavior?