我会做的是给列表框控件模板上的负边距,并给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>
我喜欢这个解决方案的简单,但使用阴性切缘影响相邻控件的布局不同的样式(在我的情况,切断文本内容)。相反,我在'ListBox'上使用了负面的顶部和底部'Padding',效果很好。 – Jay 2010-03-22 19:16:18
负填充的想法有点违反直觉,但它使得一个非常简单和简单的解决方案。 – 2010-03-22 20:38:33
是的,你需要做出正确的选择,我只是展示了一个不太容易的想法。无论您希望赋予子元素的顶部和底部边距如何,请在ListBox.ControlTemplate视觉效果上给予相同但负面的效果。 – 2010-03-22 20:38:37