locked
Distance between items in ListView

    Question

  • I am trying to create a ListView using the following item template (just columns of text):

    <DataTemplate x:Key="Measurement">
            <Grid  VerticalAlignment="Top" Margin="0" Height="30" Background="{Binding BackgroundColor}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="119"/>
                    <ColumnDefinition Width="119"/>
                    <ColumnDefinition Width="119"/>
                    <ColumnDefinition Width="119"/>
                    <ColumnDefinition Width="119"/>
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding Name          }"  Grid.Column="0" Style="{StaticResource BodyTextStyle}" TextWrapping="NoWrap"/>
                <TextBlock Text="{Binding ValueWithUnits}"  Grid.Column="1" Style="{StaticResource BodyTextStyle}" TextWrapping="NoWrap"/>
                <TextBlock Text="{Binding Status        }"  Grid.Column="2" Style="{StaticResource BodyTextStyle}" TextWrapping="NoWrap"/>
                <TextBlock Text="{Binding HighLimit     }"  Grid.Column="3" Style="{StaticResource BodyTextStyle}" TextWrapping="NoWrap"/>
                <TextBlock Text="{Binding LowLimit      }"  Grid.Column="4" Style="{StaticResource BodyTextStyle}" TextWrapping="NoWrap"/>
            </Grid>
        </DataTemplate>

    The ListView itself is defined as follows:

    <ListView              
                        Grid.Row="2" 
                        Margin="0" 
                        ItemsSource="{Binding Content}"
                        IsEnabled="False"
                        ItemTemplate="{StaticResource Measurement}" 
                        />  

    The items have a distance of about 20 pixels between them which I do not need. Setting a negative Margin value or setting the Height to a lower value does not work because it shrinks the space used for the actual text and hides some part of it. 

    Does anyone know how I can reduce the space between rows? Thanks in advance!

    Friday, November 09, 2012 11:31 AM

Answers

  • Does a negative margin on the 'ItemContainer' work in your case:

                <ListView.ItemContainerStyle>
                    <Style TargetType="ListViewItem">
                        <Setter Property="HorizontalAlignment" Value="Stretch"/>
                        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                        <Setter Property="VerticalAlignment" Value="Stretch"/>
                        <Setter Property="VerticalContentAlignment" Value="Stretch"/>
                        <!-- Adjust as required -->
                        <Setter Property="Margin" Value="0,-3,0,-3"/>
                </Style>
                </ListView.ItemContainerStyle>

    • Marked as answer by Jesse Jiang Thursday, November 15, 2012 2:48 AM
    Friday, November 09, 2012 2:55 PM

All replies

  • Does a negative margin on the 'ItemContainer' work in your case:

                <ListView.ItemContainerStyle>
                    <Style TargetType="ListViewItem">
                        <Setter Property="HorizontalAlignment" Value="Stretch"/>
                        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                        <Setter Property="VerticalAlignment" Value="Stretch"/>
                        <Setter Property="VerticalContentAlignment" Value="Stretch"/>
                        <!-- Adjust as required -->
                        <Setter Property="Margin" Value="0,-3,0,-3"/>
                </Style>
                </ListView.ItemContainerStyle>

    • Marked as answer by Jesse Jiang Thursday, November 15, 2012 2:48 AM
    Friday, November 09, 2012 2:55 PM
  • Worked perfectly, thanks!
    Tuesday, November 20, 2012 9:00 AM