WPF ItemsControl 中的项目间距

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20971831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:26:31  来源:igfitidea点击:

Item spacing in WPF ItemsControl

wpfxaml.net-4.0itemscontrol

提问by Denys Wessels

I'm displaying a List<string>collection in an ItemsControl. The problem is that there is no spacing between the list items such as TheyAreAllNextToEachOther.

List<string>在 ItemsControl 中显示一个集合。问题在于列表项之间没有间距,例如TheyAreAllNextToEachOther.

How can I create some spacing between the items?

如何在项目之间创建一些间距?

<ItemsControl Grid.Column="2" 
         Grid.ColumnSpan="2" 
         ItemsSource="{Binding Path=ShowTimes}"
         BorderThickness="0">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel IsItemsHost="True" Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

采纳答案by gomi42

I'd add an ItemTemplate where you set the margin

我会在你设置边距的地方添加一个 ItemTemplate

<ItemsControl.ItemTemplate>
    <DataTemplate>
        <TextBlock Margin="3,3,3,3" Text="{Binding}"/>
   </DataTemplate>
</ItemsControl.ItemTemplate>

回答by Rohit Vats

Provide style to your ItemsControl containers (default ContentPresenter) like this where you can set Marginto say 5:

像这样为您的 ItemsControl 容器(默认 ContentPresenter)提供样式,您可以将 Margin 设置为 5:

    <ItemsControl>
        <ItemsControl.ItemContainerStyle>
            <Style>
                <Setter Property="FrameworkElement.Margin" Value="5"/>
            </Style>
        </ItemsControl.ItemContainerStyle>
    </ItemsControl>