Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Retain selected item in picker after re-opening the page

$
0
0

I am using below code. But selected item is not retaining after re-opening the page

   <StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand">
                <Picker Title="Select Root Storage" FontFamily="Bold"
                 ItemsSource="{Binding Storages}"
                 ItemDisplayBinding="{Binding value}"
                 SelectedItem="{Binding SelectedStorage, Mode=TwoWay}"
                 />
               <Label x:Name="lblStorage" Text="{Binding Storage }" TextColor="Teal"></Label>
            </StackLayout>

Below is my viewmodel code

public class SettingsViewModel : INotifyPropertyChanged
    {
        private Storage _selectedStorage;

        private string _storage;

        public string Storage
        {

            get { return _storage; }
            set
            {
                if (_storage != value)
                {
                    _storage = value;
                    OnPropertyChanged();
                }
            }
        }

        public List<Storage> Storages { get; set; }

        public Storage SelectedStorage
        {
            get { return _selectedStorage; }
            set
            {
                if (_selectedStorage != value)
                {
                    _selectedStorage = value;

                }


                    Storage = "Root Storage: " + _selectedStorage.value;

            }

        }

        protected void OnPropertyChanged([CallerMemberName] string name = null)
        {
            PropertyChanged.Invoke(this, new PropertyChangedEventArgs(name));
        }
        public SettingsViewModel()
        {
            Storages = new List<Storage>()
            {
              new Storage(){key=1,value="Internal Storage"},
              new Storage(){key=2,value="External Storage"}
            };


        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

    public class Storage
    {
        public int key { get; set; }
        public string value { get; set; }
    }

Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>