wpfxamllistboxitemtemplate

Listbox items not wrapping, tried almost everything. it might be due to ItemTemplate


**Tried adding wrap panel as parent, as child, as item template parent, but didn't work **

    <UserControl.Resources>
        <DataTemplate x:Key="CredentialTemplate" DataType="{x:Type local:Credentials}">
            <WrapPanel Width="800" Orientation="Horizontal">
                <Grid Background="Red" Width="160" Height="160">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="40"/>
                        <RowDefinition/>
                        <RowDefinition/>

                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>

                    <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding FileName}" FontWeight="Bold" Margin="0,0,10,0"/>
                    <Button Grid.Row="1" Grid.Column="0" x:Name="btnCopyUsername" Tag="{Binding Button}" Content="Copy Username" Click="OnCopyUsernameButtonClicked" Margin="10"/>
                    <Button Grid.Row="1" Grid.Column="1"  x:Name="btnCopyPassword" Tag="{Binding Button}" Content="Copy Password" Click="OnCopyPasswordButtonClicked" Margin="10"/>
                    <Button Grid.Row="2" Grid.Column="0" x:Name="btnEdit" Tag="{Binding Button}" Content="Edit" Click="OnEditButtonClicked" Margin="10"/>
                    <Button Grid.Row="2" Grid.Column="1" x:Name="btnCopyBoth" Tag="{Binding Button}" Content="Copy Both" Click="OnCopyBothButtonClicked" Margin="10"/>

                </Grid>

            </WrapPanel>
 

        </DataTemplate>
    </UserControl.Resources>
    <WrapPanel Width="900" Background="Yellow">


        <ListBox ItemsSource="{Binding Credentials}" ItemTemplate="{StaticResource CredentialTemplate}" ScrollViewer.HorizontalScrollBarVisibility="Disabled"  ScrollViewer.VerticalScrollBarVisibility="Disabled">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Horizontal" Background="Green" Margin="10"/>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
        </ListBox>


    </WrapPanel>
    
    
</UserControl>

It might be the grid control that not letting wrap, normally without item template it should work

Screenshot


Solution

  • I explored this issue with a simplified version of your markup. I don't have a credentials class, but what's in the grids doesn't matter.

    My markup has less wrappanels:

        <Grid>
        <Grid.Resources>
            <DataTemplate x:Key="CredentialTemplate">
                    <Grid Background="Red" Width="40" Height="40">
    
                    </Grid>
            </DataTemplate>
        </Grid.Resources>
        <ListBox ItemsSource="{Binding Points}" 
                 ItemTemplate="{StaticResource CredentialTemplate}" 
                 ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
                     ScrollViewer.VerticalScrollBarVisibility="Disabled">
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel Orientation="Horizontal" 
                                   Background="Green" Margin="10"/>
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
        </ListBox>
    </Grid>
    

    I've got 30 Points in my viewmodel. My grid is smaller

    Wraps ok:

    enter image description here