locked
How to set default value to picker RRS feed

  • Question

  • User325400 posted

    Hi All,

    I am able to bind values to picker which is inside listview. But facing difficult to set default value to picker. I tried by setting SelectedIndex="0" but no luck. Below is my code in XAML.

        <Picker x:Name="pckdate" ItemsSource="{Binding clsrentals}"  ItemDisplayBinding="{Binding strdates}"  TextColor="#348e8a" Grid.Row="0" Grid.Column="3"  Title="Choose date" FontSize="12"   HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" >
                                                <Picker.SelectedIndex>0</Picker.SelectedIndex>
        </Picker>
    

    XAml.cs

         List<ClsRenWeekly> ClsRenWeeklys = new List<ClsRenWeekly>
                    {
                        new ClsRenWeekly { strdates = "Sunday", strdateid = 0 },
                        new ClsRenWeekly { strdates = "Monday", strdateid = 1 },
                        new ClsRenWeekly { strdates = "Tuesday", strdateid = 2 }
                    };
    
    
                    ListItemModels = new ObservableCollection<ListItemModel>
                {
                    new ListItemModel{ Strbtnno="B1", blnrenew= false, strrent= "70", strsttartper="Jan", clsrentals = ClsRenWeeklys  },
                    new ListItemModel{ Strbtnno="A1", blnrenew= true, strrent= "70", strsttartper="Jan", clsrentals = ClsRenWeeklys  },
                    new ListItemModel{ Strbtnno="A2", blnrenew= false, strrent= "70", strsttartper="Jan", clsrentals = ClsRenWeeklys  },
                    new ListItemModel{ Strbtnno="A3", blnrenew= true, strrent= "70", strsttartper="Jan", clsrentals = ClsRenWeeklys  },
    
                };
    
                    lstrenewal.ItemsSource = ListItemModels;
                }
    

    Can anyone please suggest.

    Wednesday, September 5, 2018 6:17 PM

Answers

  • User325400 posted

    Hi All, @Bushbert , @ColeX

    I got solution and changed my code as below In xaml

    ItemsSource="{Binding clsrentals}" SelectedItem="{Binding strdateid, Mode=TwoWay}" SelectedIndex="{Binding IndexValue}" ItemDisplayBinding="{Binding strdates}"

    and in .cs file i am passing value of IndexValue as 0

    Thank You

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, September 11, 2018 4:58 PM

All replies

  • User369520 posted

    Set the selected index in the code behind rather than in the xaml. This ensures the items have been populated before you set it.

    Wednesday, September 5, 2018 9:24 PM
  • User369978 posted

    @Bushbert said: Set the selected index in the code behind rather than in the xaml. This ensures the items have been populated before you set it.

    As BushBert said, we should set selected index in the code behind rather than in the xaml.

    Code behind

     pckdate.SelectedIndex = 0;
    

    But i prefer you to use Picker.SelectedItem Property, It can be bind to an object in your viewModel which is more close to MVVM pattern.You just need to initialize value to that object ,then picker can get the default value.

    Check sample here :https://developer.xamarin.com/samples/xamarin-forms/UserInterface/BindablePicker/

    Thursday, September 6, 2018 4:23 AM
  • User325400 posted

    @Bushbert said: Set the selected index in the code behind rather than in the xaml. This ensures the items have been populated before you set it.

    Hi, Thanks for the suggestion, since picker is inside listview. in code behind we can't find picker name x:Name="pckdate" please suggest how to get picker name in code behind de listview. in code behind we can't find picker name x:Name="pckdate"

    Thursday, September 6, 2018 11:51 AM
  • User369978 posted

    @DarshanJS said:

    @Bushbert said: Set the selected index in the code behind rather than in the xaml. This ensures the items have been populated before you set it.

    Hi, Thanks for the suggestion, since picker is inside listview. in code behind we can't find picker name x:Name="pckdate" please suggest how to get picker name in code behind de listview. in code behind we can't find picker name x:Name="pckdate"

    I'm sorry for not seeing the picker inside listview . We can't be able to access the control by x:name inside Listview dataTemplate, here is detailed explanation.

    I'm afraid you have to follow the second way i suggest .

    Friday, September 7, 2018 1:26 AM
  • User325400 posted

    @ColeX said: I'm sorry for not seeing the picker inside listview . We can't be able to access the control by x:name inside Listview dataTemplate, here is detailed explanation.

    I'm afraid you have to follow the second way i suggest .

    Thanks, I will try second way what you suggested.

    Friday, September 7, 2018 9:49 AM
  • User325400 posted

    Hi All, @Bushbert , @ColeX

    I got solution and changed my code as below In xaml

    ItemsSource="{Binding clsrentals}" SelectedItem="{Binding strdateid, Mode=TwoWay}" SelectedIndex="{Binding IndexValue}" ItemDisplayBinding="{Binding strdates}"

    and in .cs file i am passing value of IndexValue as 0

    Thank You

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, September 11, 2018 4:58 PM