在WPF/Silverlight ListBox中的项目之间添加空格,而不是在第一个或最后一个之上的空格

Jay*_*Jay 11 .net silverlight wpf xaml listbox

是否有标准/最佳方式来在WPF中空间项目ListBox,以便连续项之间有空格,但不高于第一个或低于最后一个?

对我来说,添加间距最明显的方法是修改ItemTemplate每个项目上方和下方的上方,下方或两者的包含空间.当然,这意味着第一个和/或最后一个项目上方/下方的空间或单个项目.

我可以使用触发器为第一个,最后一个,中间项目选择不同的模板,但是想知道是否有更简单的东西我不知道 - 似乎控制间距是一个常见的要求.

谢谢.

注意:我在WPF中工作,但假设这与Silverlight XAML中的相似(如果不相同).

Job*_*Joy 11

我要做的是,在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>
Run Code Online (Sandbox Code Playgroud)