s3kt0r
s3kt0r

Reputation: 1243

ListView spacing in XAML

So, I am having issues with spacing in a listview, Here is my code:

            <ListView Width="300" ItemsSource="{Binding ChosenDepartment.HoursList}" Grid.Row="1" Grid.Column="1" Margin="100,0,0,0" IsItemClickEnabled="False" SelectionMode="None">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Width="125" Text="{Binding Day}" Style="{StaticResource BasicTextStyle}" FontWeight="Bold"/>
                        <TextBlock Width="160" Text="{Binding Time}" Style="{StaticResource BasicTextStyle}"/>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="Padding" Value="0"/>
                    <Setter Property="Margin" Value="0"/>
                </Style>
            </ListView.ItemContainerStyle>
        </ListView>

No matter what I do, there is spacing underneath the StackPanel between ListItems. Enough room for two lines of text. In fact, if the text in a TextBlock wraps into two lines, it fills the ListViewItem perfectly.

I can adjust the margin and Padding on all the TextBlocks and it does nothing. The only way to effect the height of a ListView item is if I had a height property added to the ItemContainerStyle. If I hard code a Height, it affects it, but of course, I don't want to hard code the Height.

Upvotes: 1

Views: 2171

Answers (2)

s3kt0r
s3kt0r

Reputation: 1243

Jeremy's response led me down the right track, but ultimately I discovered another control called ItemsControl that comes without all the fluff of ListView and was exactly what I was looking for.

Upvotes: 2

Jeremy Gilbert
Jeremy Gilbert

Reputation: 361

ListViewItems have hard-coded margins inside their template to accomodate the borders and visual indications that are added when you select them (by right clicking them or swiping down on them with a finger).

If you want to remove them, you need to edit their template in Blend: right click on your ListView then select Edit Additionnal Templates > Edit Generated Item Container (ItemContainerStyle) > Edit a copy... and modify the template.

Upvotes: 2

Related Questions