我要做的是,在 ListBox 控件模板上给予负边距,并为 DataTemplate/ItemContainerStyle 提供相同的边距。这使得第一个和最后一个项目的空间。检查以下 XAML。相反,我在 DataTemplate 中设置了 Button(ListBoxItem) 本身的边距。
<Windows.Resources>
<ControlTemplate x:Key="ListBoxControlTemplate1" TargetType="{x:Type ListBox}">
<Grid Background="{TemplateBinding Background}">
<ItemsPresenter Margin="0,-5"/>
</Grid>
</ControlTemplate>
</Window.Resources>
<ListBox HorizontalAlignment="Center" VerticalAlignment="Center" Template="{DynamicResource ListBoxControlTemplate1}" Background="#FFAB0000">
<Button Content="Button" Width="88" Margin="0,5"/>
<Button Content="Button" Width="88" Margin="0,5"/>
<Button Content="Button" Width="88" Margin="0,5"/>
<Button Content="Button" Width="88" Margin="0,5"/>
<Button Content="Button" Width="88" Margin="0,5"/>
</ListBox>