MMM
MMM

Reputation: 321

WPF Listview Item alignment

I'm fairly new to WPF.

And i want to know how to align items side by side but still Horizontal.

So lets sat this is my items right now:

( 0 ) ( 0 ) ( 0 ) ( 0 ) ( 0 ) 

But if there isn't more space it will just show a scrollbar instead of continuing on the next line like this:

( 0 ) ( 0 ) ( 0 )
( 0 ) ( 0 )

This is my XAML:

<ListView x:Name="listViewResourceHours" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderThickness="0" SelectionMode="Single" Height="100" Width="500" >
    <ListView.GroupStyle>
        <GroupStyle>
            <GroupStyle.Panel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </GroupStyle.Panel>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <TextBlock FontWeight="Bold" Text="{Binding Name, StringFormat={}{0}:}" />
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="GroupItem">
                                <StackPanel Orientation="Horizontal">
                                    <ContentPresenter Margin="0,0,0,0" VerticalAlignment="Center" />
                                    <ItemsPresenter Margin="0,0,0,0" VerticalAlignment="Center"/>
                                </StackPanel>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GroupStyle.ContainerStyle>
        </GroupStyle>
    </ListView.GroupStyle>
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled" />
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center" >
                <Label VerticalAlignment="Center"  Margin="0" Content="{Binding Hours}" />
                <Label Name="lblWorkingHours" VerticalAlignment="Center"  Margin="0,0,0,0" Content="{Binding WorkingHoursType, Converter={StaticResource ResourceKey=hoursTypeConverter}}" />
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>

</ListView>

The code is from this question:

Arranging ListView items Horizontally

Upvotes: 3

Views: 3087

Answers (2)

blindmeis
blindmeis

Reputation: 22445

what about a wrappanel?

<ListView.ItemsPanel>
    <ItemsPanelTemplate>
        <WrapPanel Orientation="Horizontal" />
    </ItemsPanelTemplate>
</ListView.ItemsPanel>

Upvotes: 1

Johannes Wanzek
Johannes Wanzek

Reputation: 2875

Use a WrapPanel with a fixed size or the size of your ListViewItem.

Upvotes: 2

Related Questions