Listbox Vertical Gap between Items (Remove??)

Viewed 1728

My listboxes are data driven from Lists of objects with databinding using a DataTemplate eg:

            <ListBox x:Name="TheMainListBox" 
                 ScrollViewer.IsVerticalRailEnabled="True" 
                 ScrollViewer.IsHorizontalRailEnabled="False" 
                 HorizontalAlignment="Left" 
                 VerticalAlignment="Top"
                 Height="540" 
                 ItemsSource="{Binding}"
                 Width="Auto"
                 Margin="0"
                 Padding="0"
                 Background="Yellow"
                 ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
                 ScrollViewer.VerticalScrollBarVisibility="Auto" 
                 SelectionChanged="TheMainListBox_SelectionChanged" 
                 DoubleTapped="TheMainListBox_DoubleTapped"  
                 >

The template:

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid or Stackpanel Background="Blue"
                         Padding="0"                                    
                         BorderBrush="Black"
                         BorderThickness="1"
                         Margin="0"
                     >
                     .... Binding Textboxes/blocks
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>

I end up with a yellow container, the ListBox, .. with blue rectangles inside, the ListBox items but there is a vertical gap between them. I can set a negative vertical-top margin but that is cruddy and doesn't work for the top item. How can I reduce the vertical gap between items to zero.

If a create a ListBox with static items, each in a ListBoxItem container, eg:

                <ListBoxItem BorderThickness="1" Width="100" Height="50"
                  BorderBrush="Black" Background="Red"/>

It all works as required.

So how can I get rid of the vertical spacing between items with the ItemTemplate/DataBinding? This is mission critical, thx in advance.

3 Answers
Related