Windows8 ListView and space between items
Asked Answered
A

6

18

I want to change spacing between items in listView (maybe i should use another View-element?) Code looks like this:

    <ListView SelectionMode="None" HorizontalContentAlignment="Left" >
        <ListView.Items>
            <TextBlock Text="Item 1" />
            <TextBlock Text="Item 2" />
            <TextBlock Text="Item 3" />
            <TextBlock Text="Item 4" />
        </ListView.Items>
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapGrid Orientation="Horizontal" HorizontalChildrenAlignment="left"/>
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
    </ListView>

I want to imitate as much as possible normal stackpanel (wchich can wrap elements). Currently spaces (horizontal space) beetween items are far too big. My previous question -> Windows 8 WrapPanel

Thanks in advance

Allegory answered 21/7, 2012 at 18:26 Comment(0)
T
30

You will need to make changes to the default template. If you just want to make simple changes such as the padding and margins then you can do this: (tested the code and should work)

            <ListView>
        <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapGrid Orientation="Horizontal" HorizontalChildrenAlignment="left"/>
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>
        <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="Padding" Value="0"/>
                    <Setter Property="Margin" Value="0"/>
                </Style>
            </ListView.ItemContainerStyle>
            <ListViewItem>
                <TextBlock Foreground="Wheat">hej</TextBlock>
            </ListViewItem>
            <ListViewItem>
                <TextBlock Foreground="Wheat">hej</TextBlock>
            </ListViewItem>
        </ListView>

For more control make a copy of the whole default template by selecting a listviewitem in the designer and rightclicking. select edit template, edit copy. This will generate the default template and you can make your changes there. You can do the same for the listviewcontainer. You can also do this by using Blend.

I've added a description and images here (how you can edit a default template)

Let me know how it goes, and if you have more questions! Good luck!

EDIT:

MemeDeveloper mentioned below that he still had issues and solved it by tweaking some other properties- he posted a code and answer here, make sure to take a look.

Tucker answered 22/7, 2012 at 0:43 Comment(6)
Thanks for help :) One more question. I was almost sure that it should be done int his way but I couldn't find any tool where i could edit that styles. How can I do this in Blend for example?Allegory
I've posted a blog-post where I've included description and images for how to do it in VS or blend :) Hope it helps! Don't hesitate to ask more questions etc. Link: irisclasson.com/2012/07/22/…Tucker
NP, glad to be able to help :) Have fun programming :)Tucker
I had to do <Setter Property="Margin" Value="0,-4"/> to get this to work for me.Marjorymarjy
I think the "proper" / best way to do this is in my answer https://mcmap.net/q/648457/-windows8-listview-and-space-between-items I had problems with all the other solutions I've seen.Abiding
This works very well for ListBox too (if you change the TargetType to ListBoxItem of course!Wessel
A
23

As far as I know (windows8.1 xaml store app), after setting these two to zero

<ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="Padding" Value="0"/>
                    <Setter Property="Margin" Value="0"/>

I still had frustrating gaps I seemingly could not get rid of.

Using negative margins here might do it for you but... its hacky, hard to work with, and will not always lead to expected results

After pulling my hair out I found the issue was resolved with these guys i.e.

ContentMargin="0" Padding="0"

In the ListViewItemPresenter as follows :

        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="Padding" Value="0" />
                <Setter Property="Margin" Value="0" />
                <Setter Property="BorderThickness" Value="0" />
                <Setter Property="VerticalContentAlignment" Value="Top" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListViewItem">
                            <ListViewItemPresenter ContentMargin="0" Padding="0" />
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListView.ItemContainerStyle>

Hope that saves someone else from smashing their head repeatedly on their keyboard ;)

Abiding answered 22/5, 2014 at 22:14 Comment(3)
You saved my day, mate :-):-). I was trying to find this since many days.Courtmartial
I've spent hours on this, and would have spent days if I hand't found this!Hand
I still needed a negative ContentMargin to reduce the margins further.Hand
K
13

I think the correct ItemContainerStyle Property to modify here is MinHeight, not Margin or Padding.

Knowhow answered 10/11, 2015 at 6:18 Comment(2)
Spot on! This worked for me after a whole bunch of trial and error.Silvestro
Wow, not only it works great for original purpose, but also it works wonderfully when the window size is reduced: previously, the some bottom list items were clipped, and now, it doesn't!Differential
T
2

You need to edit the ListBox.ItemContainerTemplate property. Blend or VS visual designer helps extract it for modification.

Thrombin answered 21/7, 2012 at 20:28 Comment(1)
...yes and particularly, within it the ListViewItemPresenter's ContentMargin and Padding propertiesAbiding
M
1

Solution for eleminating margins in GridView items for Windows 8.1 Store App. Inspired by above ListView solution.

<GridView.ItemContainerStyle>
<Style TargetType="GridViewItem">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="GridViewItem">
                <GridViewItemPresenter ContentMargin="0" Padding="0"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
</GridView.ItemContainerStyle>
Musaceous answered 19/11, 2014 at 10:19 Comment(0)
E
1

For WinUI3 these few lines helped me to get rid of space between rows:

                <ListView.ItemContainerStyle>
                    <Style TargetType="ListViewItem">
                        <Setter Property="MinHeight" Value="0" />
                    </Style>
                </ListView.ItemContainerStyle>

I hope to help someone.

Euchromosome answered 20/12, 2022 at 22:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.