windows-phone-7windows-phone-8longlistselector

LongListSelector - Value does not fall within the expected range


I've upgraded an app from wp7 to 8 and am now getting a System.ArgumentException "Value does not fall within the expected range." The app throws the exception after exiting the method where I set the ItemsSource for a LongListSelector control. The StackTrace isn't available from the ExceptionObject and this works without any issues in wp7.

As a test, I have done the following in the Loaded event for the application:

 private void PhoneApplicationPage_Loaded_1(object sender, RoutedEventArgs e)
        {
            List<string> test = new List<string>();
            test.Add("hi");
            BrowseListBox.ItemsSource = test;
        }

After the method is completed the exception is thrown.

Here is my XAML for the LongListSelector. The TextBlock Text properties were previously Binding but have been replaced with static values until I get this working.

<phone:LongListSelector Name="BrowseListBox" IsGroupingEnabled="True" LayoutMode="Grid">
                    <phone:LongListSelector.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Margin="0,0,0,17">
                                <TextBlock Text="test" TextWrapping="Wrap" FontSize="29" Tap="TextBlock_Tap_1" />
                            </StackPanel>
                        </DataTemplate>
                    </phone:LongListSelector.ItemTemplate>
                    <phone:LongListSelector.GroupHeaderTemplate>
                        <DataTemplate>
                            <Border>
                                <TextBlock Text="test" FontSize="32" Foreground="White"  />
                            </Border>
                        </DataTemplate>
                    </phone:LongListSelector.GroupHeaderTemplate>
                    <phone:LongListSelector.JumpListStyle>
                        <Style TargetType="phone:LongListSelector">
                            <Setter Property="ItemTemplate">
                                <Setter.Value>
                                    <DataTemplate>
                                        <Border Name="JumpListBorder" BorderThickness="0,0,0,1">
                                            <TextBlock Text="test" FontSize="50" Foreground="White"  />
                                        </Border>
                                    </DataTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </phone:LongListSelector.JumpListStyle>
                </phone:LongListSelector>

Has anyone seen this before?


Solution

  • It's because you're using LayoutMode Grid

    You can either not use the grid, or specify the GridCellSize property, and then it works:

    <phone:LongListSelector Name="BrowseListBox" IsGroupingEnabled="True" LayoutMode="Grid" GridCellSize="50,50">