Dot-Net
在 WPF/Silverlight ListBox 中的項目之間添加空格,在第一個上方或最後一個下方沒有空格
是否有標準/最佳方式來分隔 WPF 中的項目
ListBox,以便連續項目之間有空間,但不高於第一個或低於最後一個?對我來說,添加間距最明顯的方法是修改
ItemTemplate以在每個項目的上方、下方或上方和下方包含空間。當然,這意味著在第一個和/或最後一個項目或單個項目的上方/下方也會有空間。我可以使用觸發器為第一個、最後一個、中間項目選擇不同的模板,但想知道是否有一些更簡單的東西我錯過了——似乎控制間距是一個常見的要求。
謝謝。
注意:我在 WPF 中工作,但假設這在 Silverlight XAML 中是相似的(如果不相同)。
我要做的是,在 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>