windows-phone-8indexingselecteditemlistpicker

how to keep toolkit:listPicker selectedItem at a new page load WP8


I am using toolkit:listPicker for selection of vehicle from a list Collection, but when I go to another page and get back to the one with the listPicker tool, the selected index is changed to '0' (it is '-1' by default when the list is empty). Please see my methods below:

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
                ReadAllVehicleList dbVehicles = new ReadAllVehicleList();
                DB_VehicleList = dbVehicles.GetAllVehicles();
                pickVehicle.ItemsSource = DB_VehicleList.OrderByDescending(i => i.Id).ToList();
    }

This one is calling following:

    public class ReadAllVehicleList
{
    DatabaseHelperClass Db_Helper = new DatabaseHelperClass();
    public ObservableCollection<Vehicle> GetAllVehicles()
    {
        return Db_Helper.ReadVehicle();
    }
}

    //This class for perform all database CRUID operations
    public class DatabaseHelperClass
{
        SQLiteConnection dbConn;

        public ObservableCollection<Vehicle> ReadVehicle(){
        using (var dbConn = new SQLiteConnection(App.DB_PATH))
        {
            List<Vehicle> myCollection = dbConn.Table<Vehicle>().ToList<Vehicle>();
            ObservableCollection<Vehicle> VehicleList = new ObservableCollection<Vehicle>(myCollection);
            return VehicleList;
        }
}

And one selectionChanged event to get the selected vehicle and display all refuelings for it in a listBox tool:

        private void pickVehicle_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
            if (pickVehicle.SelectedIndex != -1)
            {
            VehiclesPage.selectedVehicle = pickVehicle.SelectedItem as Vehicle;//selectedVehicle присвоява избраното превозно средство.
            ReadAllRefuelingList dbRefuelings = new ReadAllRefuelingList();
            DB_RefuelingList = dbRefuelings.GetAllRefuelings();//Извличане на всички записи за зареждане на гориво от базата данни
            listBoxobj.ItemsSource = DB_RefuelingList.OrderByDescending(i => i.Id).ToList();//Показване на записите в обратен ред (descending) - най-новото ID се показва най-отгоре
            }
    }

XAML:

        <toolkit:ListPicker x:Name="pickVehicle"     HorizontalAlignment="Center" Margin="0" VerticalAlignment="Top" Grid.Row="1" Width="480" SelectionChanged="pickVehicle_SelectionChanged" Background="Gray" BorderBrush="Orange" Foreground="White">
        <toolkit:ListPicker.ItemTemplate>
            <DataTemplate>
                <TextBlock HorizontalAlignment="Center"> 
                                <Run Text="{Binding Make}"/>
                                <Run Text="{Binding Model}"/>
                </TextBlock>
            </DataTemplate>
        </toolkit:ListPicker.ItemTemplate>
        </toolkit:ListPicker>

Here is an example of the problem. I have 2 vehicles in the Database/listPicker. Index'0' = BMW E46 and Index'1' = Opel Astra. I have selected Opel Astra (Index'1'), but when i go to another page and then back to this one, the index is goind back to '0'.

Image_1


Solution

  • The ItemsSource of the ListPicker is set inside the Loaded event of the page.

       private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
            {       
                 ReadAllVehicleList dbVehicles = new ReadAllVehicleList();
                 DB_VehicleList = dbVehicles.GetAllVehicles();           
                 pickVehicle.ItemsSource = DB_VehicleList.OrderByDescending(i => i.Id).ToList();
            }
    

    The Loaded event will be fired again when you come back to this page and hence the ItemsSource will be reset, changing the selected item.

    Based on your requirement you can either shift the code to set ItemSource to some other place or may be store the SelectedIndex of the ListPicker and reassign while coming back.