列表框项目之间的垂直间隙(删除?)

Dav*_*nes 5 c# xaml listbox win-universal-app

我的列表框是从使用 DataTemplate 进行数据绑定的对象列表驱动的数据,例如:

            <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"  
                 >
Run Code Online (Sandbox Code Playgroud)

模板:

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid or Stackpanel Background="Blue"
                         Padding="0"                                    
                         BorderBrush="Black"
                         BorderThickness="1"
                         Margin="0"
                     >
                     .... Binding Textboxes/blocks
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
Run Code Online (Sandbox Code Playgroud)

我最终得到一个黄色容器,即列表框,..里面有蓝色矩形,即列表框项目,但它们之间有一个垂直间隙。我可以设置负的垂直顶部边距,但这很粗糙,并且不适用于顶部项目。如何将项目之间的垂直间隙减小到零。

如果创建一个包含静态项的 ListBox,每个项都位于 ListBoxItem 容器中,例如:

                <ListBoxItem BorderThickness="1" Width="100" Height="50"
                  BorderBrush="Black" Background="Red"/>
Run Code Online (Sandbox Code Playgroud)

一切都按要求进行。

那么如何使用 ItemTemplate/DataBinding 消除项目之间的垂直间距呢? 这是关键任务,请提前致谢。

Chr*_* W. 5

我没有时间加载项目进行测试,但您应该能够删除可能导致它的边距/填充。所以添加;

<ListBox.ItemContainerStyle>
   <Style TargetType="ListBoxItem">
      <Setter Property="Padding" Value="0"/>
      <Setter Property="Margin" Value="0"/>
   </Style>
</ListBox.ItemContainerStyle>
Run Code Online (Sandbox Code Playgroud)