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

huangapple go评论59阅读模式
英文:

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

问题

尝试将WrapPanel添加为父元素、子元素以及项目模板的父元素,但不起作用

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

可能是Grid控件不允许包装,通常情况下,没有项目模板的情况下应该可以工作。

屏幕截图

英文:

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

    &lt;UserControl.Resources&gt;
&lt;DataTemplate x:Key=&quot;CredentialTemplate&quot; DataType=&quot;{x:Type local:Credentials}&quot;&gt;
&lt;WrapPanel Width=&quot;800&quot; Orientation=&quot;Horizontal&quot;&gt;
&lt;Grid Background=&quot;Red&quot; Width=&quot;160&quot; Height=&quot;160&quot;&gt;
&lt;Grid.RowDefinitions&gt;
&lt;RowDefinition Height=&quot;40&quot;/&gt;
&lt;RowDefinition/&gt;
&lt;RowDefinition/&gt;
&lt;/Grid.RowDefinitions&gt;
&lt;Grid.ColumnDefinitions&gt;
&lt;ColumnDefinition/&gt;
&lt;ColumnDefinition/&gt;
&lt;/Grid.ColumnDefinitions&gt;
&lt;TextBlock Grid.Row=&quot;0&quot; Grid.Column=&quot;0&quot; Text=&quot;{Binding FileName}&quot; FontWeight=&quot;Bold&quot; Margin=&quot;0,0,10,0&quot;/&gt;
&lt;Button Grid.Row=&quot;1&quot; Grid.Column=&quot;0&quot; x:Name=&quot;btnCopyUsername&quot; Tag=&quot;{Binding Button}&quot; Content=&quot;Copy Username&quot; Click=&quot;OnCopyUsernameButtonClicked&quot; Margin=&quot;10&quot;/&gt;
&lt;Button Grid.Row=&quot;1&quot; Grid.Column=&quot;1&quot;  x:Name=&quot;btnCopyPassword&quot; Tag=&quot;{Binding Button}&quot; Content=&quot;Copy Password&quot; Click=&quot;OnCopyPasswordButtonClicked&quot; Margin=&quot;10&quot;/&gt;
&lt;Button Grid.Row=&quot;2&quot; Grid.Column=&quot;0&quot; x:Name=&quot;btnEdit&quot; Tag=&quot;{Binding Button}&quot; Content=&quot;Edit&quot; Click=&quot;OnEditButtonClicked&quot; Margin=&quot;10&quot;/&gt;
&lt;Button Grid.Row=&quot;2&quot; Grid.Column=&quot;1&quot; x:Name=&quot;btnCopyBoth&quot; Tag=&quot;{Binding Button}&quot; Content=&quot;Copy Both&quot; Click=&quot;OnCopyBothButtonClicked&quot; Margin=&quot;10&quot;/&gt;
&lt;/Grid&gt;
&lt;/WrapPanel&gt;
&lt;/DataTemplate&gt;
&lt;/UserControl.Resources&gt;
&lt;WrapPanel Width=&quot;900&quot; Background=&quot;Yellow&quot;&gt;
&lt;ListBox ItemsSource=&quot;{Binding Credentials}&quot; ItemTemplate=&quot;{StaticResource CredentialTemplate}&quot; ScrollViewer.HorizontalScrollBarVisibility=&quot;Disabled&quot;  ScrollViewer.VerticalScrollBarVisibility=&quot;Disabled&quot;&gt;
&lt;ListBox.ItemsPanel&gt;
&lt;ItemsPanelTemplate&gt;
&lt;WrapPanel Orientation=&quot;Horizontal&quot; Background=&quot;Green&quot; Margin=&quot;10&quot;/&gt;
&lt;/ItemsPanelTemplate&gt;
&lt;/ListBox.ItemsPanel&gt;
&lt;/ListBox&gt;
&lt;/WrapPanel&gt;
&lt;/UserControl&gt;

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

Screenshot

答案1

得分: 0

我使用了你的标记的简化版本来探讨这个问题。
我没有凭证类,但格子里的内容并不重要。

我的标记中有更少的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>

我的视图模型中有30个点。
我的网格较小

正常包裹:

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

英文:

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:

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

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

Wraps ok:

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

答案2

得分: 0

以下是您要翻译的内容:

<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}">
            
            <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>
英文:

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.

&lt;ListView Grid.Column=&quot;1&quot;  Grid.Row=&quot;3&quot;  Name=&quot;lv&quot; 
Style=&quot;{DynamicResource NewCollectionInfoModuleSalesRepListViewStyle}&quot;
SelectedValue=&quot;{Binding Path=SelectedSalseRepData,
Mode=TwoWay, 
UpdateSourceTrigger=PropertyChanged,
NotifyOnValidationError=True, 
ValidatesOnDataErrors=True, ValidatesOnExceptions=True}&quot;
ItemContainerStyle=&quot;{DynamicResource NewCollectionInfoModuleSalesRepListViewItemStyle}&quot;
ItemsSource=&quot;{Binding Path=lstSalesRepresentative}&quot;&gt;
&lt;ListView.ItemsPanel&gt;
&lt;ItemsPanelTemplate&gt;
&lt;WrapPanel /&gt;
&lt;/ItemsPanelTemplate&gt;
&lt;/ListView.ItemsPanel&gt;
&lt;ListView.ItemTemplate&gt;
&lt;DataTemplate&gt;
&lt;Grid&gt;
&lt;Grid.RowDefinitions &gt;
&lt;RowDefinition Height=&quot;80&quot; /&gt;
&lt;/Grid.RowDefinitions&gt;
&lt;Grid.ColumnDefinitions&gt;
&lt;ColumnDefinition Width=&quot;188&quot; /&gt;
&lt;/Grid.ColumnDefinitions&gt;
&lt;Button  Grid.Row=&quot;0&quot; Grid.Column=&quot;0&quot;
HorizontalAlignment=&quot;Stretch&quot;
VerticalAlignment=&quot;Stretch&quot;
Style=&quot;{DynamicResource NewCollectionInfoModuleSalesRepListButtonStyle}&quot;
Background=&quot;{Binding Path=SelectedSalesRepColor,Mode=TwoWay}&quot;
IsEnabled=&quot;False&quot;&gt;
&lt;StackPanel&gt;
&lt;TextBlock  Grid.Row=&quot;0&quot; Grid.Column=&quot;0&quot;
Style=&quot;{DynamicResource NewCollectionInfoModuleSalesRepListTextBlockStyle}&quot;
Text=&quot;{Binding Path=Name,Mode=TwoWay}&quot;
Foreground=&quot;{Binding Path=SelectedSalesRepTextBlockColor,Mode=TwoWay}&quot;&gt;
&lt;/TextBlock&gt;
&lt;/StackPanel&gt;
&lt;/Button&gt;
&lt;/Grid&gt;
&lt;/DataTemplate&gt;
&lt;/ListView.ItemTemplate&gt;
&lt;/ListView&gt;

huangapple
  • 本文由 发表于 2023年2月19日 23:45:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501321.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定