Set a Picker.SelectedItem on page startup

bifedefrangobifedefrango Member ✭✭
edited April 12 in Xamarin.Forms

I have this piece of code

<Picker x:Name="CategoryPicker" 
ItemsSource="{Binding Categories, Mode=TwoWay}"
SelectedItem="{Binding Path= RequestItem.Category, Mode=TwoWay}" 
HorizontalOptions="FillAndExpand"
Title="Escolha uma Categoria"
SelectedIndexChanged="OnSelectedIndexChanged"
ItemDisplayBinding="{Binding Name}"
Margin="10,20,10,0">

I'm saving an item, and when I save it, it goes to a list of items... If I want to change one of those items, I need to set the picker to the value stored in RequestItem.Category...
Since I'm binding the SelectedItem property It should automaticaly set the value of it rigth?

I've also tryed to do this on my Page constructor with this
CategoryPicker.SelectedItem = estimate?.Category;
and this
CategoryPicker.SelectedIndex = 0;
If I set a breakpoint rigth after this, CategoryPicker.SelectedIndex is equal to -1... It doesn't change to 0...

None of those seems to work... What am I doing wrong?

Answers

  • yogeshwaranmohanyogeshwaranmohan Member ✭✭
    edited April 12

    Are you confirmed when loading the picker the Categories contains any items. If there is no any item then the picker selected item will be -1.

  • bifedefrangobifedefrango Member ✭✭
    edited April 12
    BindingContext = vm = new EstimateRequestAddNewItemViewModel(Navigation, viewModel, estimate);
    InitializeComponent();
    vm.GetCategoriesCommand.Execute(null); //---> get the categories
    CategoryPicker.SelectedIndex = 0; //---> CategoryPicker.ItemsSource = null
    SubCategoryPicker.SelectedItem = estimate.SubCategory;
    

    when the page opens I have the items on Category picker so what can i do to populate CategoryPicker.ItemsSource before page loads?

  • yogeshwaranmohanyogeshwaranmohan Member ✭✭
    edited April 12

    Remove the selected index, Set RequestItem.Category value, it will work. Set your RequestItem.Category property to OnPropertyChanged.

    If not clear, refer this sample,

    <Picker x:Name="CategoryPicker"                 
    ItemsSource="{Binding Categories, Mode=TwoWay}"
    SelectedItem="{Binding Path= CategorySeletedItem, Mode=TwoWay}" 
    HorizontalOptions="FillAndExpand"
    Title="Escolha uma Categoria"
    SelectedIndexChanged="CategoryPicker_SelectedIndexChanged"
    ItemDisplayBinding="{Binding Name}"
    Margin="10,20,10,0"/>
    

    Code behind

     private List<Category> categories;
            public List<Category> Categories
            {
                get { return categories; }
                set { categories = value; }
            }
    
            private Category categorySeletedItem;
            public Category CategorySeletedItem
            {
                get { return categorySeletedItem; }
                set { categorySeletedItem = value; OnPropertyChanged(); }
            }
    
            public MainPage()
            {
                InitializeComponent();                       
    
                Categories = new List<Category>();
                for (int i = 0; i < 10; i++)
                    Categories.Add(new Category { Name = "Name" + i });
    
                CategorySeletedItem = Categories.FirstOrDefault();
    
                BindingContext = this;
            }                        
    
  • bifedefrangobifedefrango Member ✭✭

    Thanks a lot man! That didn't quite solve the problem, but indeed help! I was able to set the selected index and now it's working... I just need to save the index position and then it will be fine! Thanks man!

  • ColeXColeX Member, Xamarin Team Xamurai

    If problem gone could you accept the helpful comment as answer ?

Sign In or Register to comment.