LongListSelector滚动问题

时间:2013-11-19 15:56:31

标签: c# windows-phone-7 windows-phone-7.1

我正在开发一个Windows Phone应用程序,当我在LongListSelector中滚动时,我遇到了一种非常奇怪的行为。我的商品模板中有两个TextBlocks和一个CheckBox。 Xaml代码如下所示。

                                                   

    <DataTemplate x:Key="GroupItem">
        <Border Background="{StaticResource PhoneAccentBrush}" Margin="{StaticResource PhoneTouchTargetOverhang}" Padding="{StaticResource PhoneTouchTargetOverhang}">
            <TextBlock Text="{Binding Key}" />
        </Border>
    </DataTemplate>

    <DataTemplate x:Key="ItemTmpl">
        <Grid x:Name="gridListPanel">
            <Grid.RowDefinitions>
                <RowDefinition Height="90"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="900"/>
            </Grid.ColumnDefinitions>
            <StackPanel>

                <TextBlock Padding="50,5,0,0" Width="Auto" Text="{Binding DayName}" Grid.Row="0" Grid.Column="0" FontSize="25" HorizontalAlignment="Left" VerticalAlignment="Top" FontWeight="Bold" Opacity="{Binding Path=OpacityValue, Converter={StaticResource OpacityConvertor}}"/>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Padding="50,5,0,0" Width="Auto" Text="{Binding PushUps}" Grid.Row="1" Grid.Column="0" FontSize="17" HorizontalAlignment="Left" VerticalAlignment="Top" FontWeight="Normal" Foreground="{Binding TextColor}" Opacity="{Binding Path=OpacityValue, Converter={StaticResource OpacityConvertor}}"/>
                    <TextBlock Padding="30,5,0,0" Text="{Binding CompletedDate}" Grid.Row="0" Grid.Column="0" FontSize="15" HorizontalAlignment="Left" VerticalAlignment="Center" FontWeight="Normal"  Opacity="0.5"/>
                </StackPanel>

            </StackPanel>
            <CheckBox x:Name="chkWeek" HorizontalAlignment="Center" VerticalAlignment="Top" Grid.Row="0" Grid.Column="1"  IsChecked="{Binding IsCompleted}" Unchecked="chkWeek_Unchecked" Tap="chkWeek_Tap"/>

        </Grid>
    </DataTemplate>

<toolkit:LongListSelector  x:Name="LongList"
                                   Grid.Row="1"
                                   GroupHeaderTemplate="{StaticResource GroupHeader}"
                                   GroupItemTemplate="{StaticResource GroupItem}"
                                   ItemTemplate="{StaticResource ItemTmpl}" SelectionChanged="LongList_SelectionChanged" Grid.ColumnSpan="2"/>

我绑定了长名单:

mainItem = new List<Item>();
int counter = 19;

for (int i = 1; i <= 9; i++)
{
     switch (i)
     {
         case 1:
              mainItem.Add(new Item() { WeekName = "Week " + i.ToString(), DayName = "Week " + i.ToString() + ", Day 1", PushUps = counter + " Pushups to perform", TextColor = "White", ImageUri = new Uri("/Images/PushUpsImages/Images/w1d1.png", UriKind.Relative), OpacityValue = "0.5" });
              counter += 3;
              mainItem.Add(new Item() { WeekName = "Week " + i.ToString(), DayName = "Week " + i.ToString() + ", Day 2", PushUps = counter + " Pushups to perform", TextColor = "White", ImageUri = new Uri("/Images/PushUpsImages/Images/w1d2.png", UriKind.Relative), OpacityValue = "0.5" });
              counter += 5;
              mainItem.Add(new Item() { WeekName = "Week " + i.ToString(), DayName = "Week " + i.ToString() + ", Day 3", PushUps = counter + " Pushups to perform", TextColor = "White", ImageUri = new Uri("/Images/PushUpsImages/Images/w1d3.png", UriKind.Relative), OpacityValue = "0.5" });
              counter += 5;
              break;
       }
 }

var selected = from c in mainItem group c by c.WeekName into n select new GroupingLayer<string, Item>(n);
LongList.ItemsSource = selected;

如果LongListSelector中没有选中的项目,则不会检查CheckBox。滚动工作正常。但是当在滚动之后检查任何项目时,它会显示异常,并且在那时它再次调用此部分代码,这是在页面构造函数中。

var selected = from c in mainItem group c by c.WeekName into n select new GroupingLayer<string, Item>(n);
LongList.ItemsSource = selected;

此时选择了retun null并抛出异常。为什么在滚动时发生这种情况意味着为什么它在滚动时再次调用这部分代码?

1 个答案:

答案 0 :(得分:2)

试试这个,设置以下属性,在xaml文件中定义LongListSelector。

VirtualizingStackPanel.VirtualizationMode="Standard"

我不确定这个,但我用列表框试过了。