Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows8 ListView and space between items

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

like image 456
Krzysztof Kaczor Avatar asked Jul 21 '12 18:07

Krzysztof Kaczor


People also ask

How to add a space between listview items?

You should wrap your ListView item (say your_listview_item) in some other layout e.g LinearLayout and add margin to your_listview_item: This way you can also add space, if needed, on the right and left of the ListView item. Show activity on this post.

How to increase the spacing between the list items?

Also one more way to increase the spacing between the list items is that you add an empty view to your adapter code by providing the layout_height attribute with the spacing you require. For e.g. in order to increase the bottom spacing between your list items add this dummy view (empty view) to the end of your list items.

How do I increase the distance between two columns in listview?

Space Between Columns To increase space between columns we will use ColumnSpacing property which can accept any value from 0 to 255. For example if we set this value to 10, the distance between columns in ListView will be exactly 10 pixels.

Is it possible to set the height of list view items?

Absolutely possible, you can define a set height for list view items, however, it is recommended to set a minimum height, rather than a predefined height that never changes.


2 Answers

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.

like image 191
Iris Classon Avatar answered Oct 07 '22 16:10

Iris Classon


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 ;)

like image 20
MemeDeveloper Avatar answered Oct 07 '22 14:10

MemeDeveloper