ScottinTexas
ScottinTexas

Reputation: 181

How do I set the ListView height to the height of the contining row in XAML

I can't figure out the syntax to tell the ListView its height is the height of the row that contains the ListView, that is, the ListView's Parent. This is the xaml in the user control. I have put what I thought would work, but doesn't. I left it there so you would see what I am attempting.

<ListView Grid.Row="1" 
Height="{Binding Path=Height,RelativeSource={RelativeSource AncestorType=Grid.Row}}"
ItemsSource="{Binding Folders}"
ItemTemplate="{StaticResource FolderListTemplate}"
SelectionMode="Single"
SelectedIndex="1"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
Margin="3"
ScrollViewer.VerticalScrollBarVisibility="Visible"
ScrollViewer.CanContentScroll="True"
/>

Thanks,

Upvotes: 1

Views: 2369

Answers (1)

taquion
taquion

Reputation: 2767

Just set the RowDefinition height to Auto, here is my xaml:

<Grid Background="DimGray">
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="50"/>
        </Grid.RowDefinitions>
        <StackPanel Background="CornflowerBlue"/>
        <ListView Grid.Row="1" 
                  x:Name="ListView"
                  ItemsSource="{Binding Items}"
                  SelectionMode="Single"
                  SelectedIndex="1"
                  VerticalAlignment="Stretch"
                  HorizontalAlignment="Stretch"
                  Margin="3"
                  ScrollViewer.VerticalScrollBarVisibility="Visible"
                  ScrollViewer.CanContentScroll="True"/>

        <StackPanel Grid.Row="2" Background="CornflowerBlue"/>
    </Grid>

And the output: enter image description here

If you use star notation then the row it is goin to take the available height for the star value you gave, in this case 1* which is the same as *****. If I modify to use star value as you:

<Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="50"/>
        </Grid.RowDefinitions>

The output:

enter image description here

Hope this helps

Upvotes: 1

Related Questions