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

61 Views Asked by At

**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

2

There are 2 best solutions below

0
Andy On BEST ANSWER

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

0
Solanki Sagar On

List view Item Template with wrapping list view items in my case i have sales man list and i am displaying sales man name in round button using Wrap Panel.

<ListView Grid.Column="1"  Grid.Row="3"  Name="lv" 
      Style="{DynamicResource NewCollectionInfoModuleSalesRepListViewStyle}"
      SelectedValue="{Binding Path=SelectedSalseRepData,
                              Mode=TwoWay, 
                              UpdateSourceTrigger=PropertyChanged,
                              NotifyOnValidationError=True, 
                              ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"
      ItemContainerStyle="{DynamicResource NewCollectionInfoModuleSalesRepListViewItemStyle}"
      ItemsSource="{Binding Path=lstSalesRepresentative}">
        
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel />
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>

        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.RowDefinitions >
                        <RowDefinition Height="80" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="188" />
                    </Grid.ColumnDefinitions>

                    <Button  Grid.Row="0" Grid.Column="0"
                             HorizontalAlignment="Stretch"
                             VerticalAlignment="Stretch"
                             Style="{DynamicResource NewCollectionInfoModuleSalesRepListButtonStyle}"
                             Background="{Binding Path=SelectedSalesRepColor,Mode=TwoWay}"
                             IsEnabled="False">
                        <StackPanel>

                            <TextBlock  Grid.Row="0" Grid.Column="0"
                                                Style="{DynamicResource NewCollectionInfoModuleSalesRepListTextBlockStyle}"
                                                Text="{Binding Path=Name,Mode=TwoWay}"
                                                Foreground="{Binding Path=SelectedSalesRepTextBlockColor,Mode=TwoWay}">
                            </TextBlock>
                        </StackPanel>
                    </Button>

                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>