列表框不断重复第一项

时间:2016-06-06 14:57:47

标签: windows-10 uwp uwp-xaml

我有一个加载项目的简单列表框(在我的测试用例135中)。 我记录了所有已加载项目的ID,并且它们都有唯一的ID。 listbox datatemplate是一个usercontrol,因此在usercontrol中我还记录了ID以查看加载了哪些ID。

现在是它开始出错的地方,它只加载前10个项目(我认为最初可见的是什么),然后一遍又一遍地重复这些第一项。因此,我有135个对象,而不是135个唯一对象,它们是前10个中的一个。

您可以在此处查看日志记录(还有更多ID不可见):

enter image description here

在用户控件ID的行之后,这是它加载的唯一ID,并保持循环这10个ID,直到列表框中有135个项目。

这是整页代码              

    <Style x:Key="ListBoxItemStyle1" TargetType="ListBoxItem">
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border x:Name="LayoutRoot" BorderThickness="3">

                        <ContentControl x:Name="ContentContainer" 
                            VerticalContentAlignment="Top" 
                            HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"  
                            Margin="{TemplateBinding Padding}" 
                            Content="{TemplateBinding Content}" 
                            ContentTemplate="{TemplateBinding ContentTemplate}" 
                            Foreground="{TemplateBinding Foreground}" />
                    </Border>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Page.Resources>

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <SearchBox x:Name="sbSearch" QuerySubmitted="sbSearch_QuerySubmitted" Margin="12,12,12,0"></SearchBox>


        <ListBox x:Name="lbResults" Grid.Row="1" ItemContainerStyle="{StaticResource ListBoxItemStyle1}" Background="{x:Null}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <userControls:WantlistItem Tag="{Binding}"></userControls:WantlistItem>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Grid>

我做错了什么?

Edit1:在主页面中加载列表框

    private void Page_Loaded(object sender, RoutedEventArgs e)
    {
        if(Variables.WantsAll == null) Helpers.GetWantList();
        foreach (var v in Variables.WantsAll)
        {
            Debug.WriteLine(v.id);
        }
        Debug.WriteLine("--- USER CONTROL ID's ---");
        lbResults.ItemsSource = Variables.WantsAll;


    }

在UserControl Page_Loaded中我也记录它们。

在这个屏幕截图中,你可以看到我向下滚动,它又开始重复相同的项目(有时它会混乱,因为你可以看到第一个项目没有正确重复,它是一个不同的项目。)

请注意,在名称前面我添加了打印出来的ID,你可以看到它重复相同的ID(例如绿色专辑:1301162),甚至在我设置为ItemsSource的列表中它只存在一次(所有项目都是独一无二的。)

Scroll down

1 个答案:

答案 0 :(得分:0)

Jay Zuo - MSFT提供的评论提供了解决方案,this thread中的答案完全解决了我的问题。