Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Item spacing in WPF ItemsControl

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.

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>
like image 358
Denys Wessels Avatar asked Jan 07 '14 12:01

Denys Wessels


2 Answers

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

    <ItemsControl>
        <ItemsControl.ItemContainerStyle>
            <Style>
                <Setter Property="FrameworkElement.Margin" Value="5"/>
            </Style>
        </ItemsControl.ItemContainerStyle>
    </ItemsControl>
like image 108
Rohit Vats Avatar answered Nov 15 '22 14:11

Rohit Vats


I'd add an ItemTemplate where you set the margin

<ItemsControl.ItemTemplate>
    <DataTemplate>
        <TextBlock Margin="3,3,3,3" Text="{Binding}"/>
   </DataTemplate>
</ItemsControl.ItemTemplate>
like image 2
gomi42 Avatar answered Nov 15 '22 15:11

gomi42