Forum Xamarin.Forms

Default value for Picker

RicoWidmerRicoWidmer USMember ✭✭

I would like to show a default value in the text field of the Picker. So far I know the "Title" is automatically used but I would like to show one of the options, the Picker shows, e.g. the first one.
When creating a custom Picker I could set the "Text" attribute under Android but I would like to have a xaml solution.
Is there a way to specify the default value in a Picker?

Tagged:

Best Answer

Answers

  • RicoWidmerRicoWidmer USMember ✭✭

    Thank you for your quick answer. This indeed works. But why doesn't "SelectedIndex" work when used in xaml?
    <Picker x:Name="Type" Unfocused="TypeChanged" SelectedIndex="1" HorizontalOptions="LayoutOptions.FillAndExpand" >

  • RomanGagarskiyRomanGagarskiy RUMember

    probably it does not have items at the moment of parsing selectedIndex at that place. I've tried to place it separately after items tags and it worked
    <Picker.Items>
    5 min
    15 min
    </Picker.Items>
    <Picker.SelectedIndex>0</Picker.SelectedIndex>

  • RicoWidmerRicoWidmer USMember ✭✭

    This makes sense. Thank you.

  • hvaughanhvaughan USMember ✭✭✭

    @RomanGagarskiy
    Thanks for the help with this!

    @RicoWidmer
    Does it really make sense though...? ;)

  • Make sure you use Picker.selectedindex=.... after binding :)

  • NurhakKayaNurhakKaya USMember ✭✭

    This is how I did it, hopefully this will be useful for the developers who need this;

    I first create a grid element with a name in xaml.

                  <Grid x:Name="GridPicker"
                          BackgroundColor="White"
                          Padding="0,0,0,0">
                    </Grid>
    

    Then in the code behind I use this method to create the picker dynamically with a selected index option:

            /// <summary>
            /// Creates GridPicker
            /// </summary>
            private void CreateGridPickerDynamically()
            {
                GridPicker.Children.Clear();
    
                Picker picker = new Picker
                {
    
                    VerticalOptions = LayoutOptions.CenterAndExpand,
                    WidthRequest = 75,
                    HeightRequest = 45
                };
    
                picker.SelectedIndexChanged += Picker_SelectedIndexChanged;
    
                List<string> questionNumberArray = new List<string> { "5", "10", "20", "30" };
    
                foreach (string item in questionNumberArray)
                {
                    picker.Items.Add(item);
                }
    
                picker.SelectedIndex = 1;
    
                GridPicker.Children.Add(picker);
            }
    
  • Ajay_SBSICAjay_SBSIC USMember ✭✭✭
    edited March 2017

    Good , thanks to @NurhakKaya and @AlessandroCaliaro

  • Ajay_SBSICAjay_SBSIC USMember ✭✭✭

    Thanks

  • VenkatKondaVenkatKonda USMember ✭✭

    I am binding observablecollection(itemssource) from viewmodel to picker, how do I set selectedIndex to 0(zero) in xaml when itemssource changed in viewmodel?

  • Picker.SelectedIndex is fine in most cases, but I have the worry of not knowing my index:

    my page can be called in 2 conditions: in the first case it is called with nothing, so you have to define the selection (my pick is therefore empty by default)

    the other condition goes through the list page (which has the same values as my picker), and I call my picker page by seeing the item select in the list. so my picker must display the selected object. knowing that I do not have a local database (so I must systematically call it from a webservice, because of possible changes, the list must constantly be up to date) so I can not know my index ...

    How can I do, please?

  • Mannoo123Mannoo123 Member ✭✭

    @RomanGagarskiy said:
    probably it does not have items at the moment of parsing selectedIndex at that place. I've tried to place it separately after items tags and it worked
    <Picker.Items>
    5 min
    15 min
    </Picker.Items>
    <Picker.SelectedIndex>0</Picker.SelectedIndex>

    is it working ?

  • WindyhenWindyhen USMember ✭✭

    @EricKinoshita said:
    There is one caveat, related to RomanGagarskiy comment. If you do the following, the Picker is not selected on load:

    <Picker SelectedItem="{Binding Foo}" ItemsSource="{Binding FooOptions}" />
    
    

    But if you swap the attributes order it does preselect on load:

    <Picker ItemsSource="{Binding FooOptions}" SelectedItem="{Binding Foo}" />
    
    

    The same applies to SelectedIndex.

    I hope this gets "fixed" in the future, because I didn't expect that the order of the attributes affected the component's behavior like this.

    Holy cow, been scratching my head for few hours, the order matters duh!!!

  • @EricKinoshita said:
    There is one caveat, related to RomanGagarskiy comment. If you do the following, the Picker is not selected on load:

    <Picker SelectedItem="{Binding Foo}" ItemsSource="{Binding FooOptions}" />
    
    

    But if you swap the attributes order it does preselect on load:

    <Picker ItemsSource="{Binding FooOptions}" SelectedItem="{Binding Foo}" />
    
    

    The same applies to SelectedIndex.

    I hope this gets "fixed" in the future, because I didn't expect that the order of the attributes affected the component's behavior like this.

    Thank you for this! Solved my problem!

  • jddjdd USMember ✭✭✭

    @EricKinoshita said:
    There is one caveat, related to RomanGagarskiy comment. If you do the following, the Picker is not selected on load:

    <Picker SelectedItem="{Binding Foo}" ItemsSource="{Binding FooOptions}" />
    
    

    But if you swap the attributes order it does preselect on load:

    <Picker ItemsSource="{Binding FooOptions}" SelectedItem="{Binding Foo}" />
    
    

    The same applies to SelectedIndex.

    I hope this gets "fixed" in the future, because I didn't expect that the order of the attributes affected the component's behavior like this.

    SelectedIndex does not work for me even after binding. I had to add a line in the .cs code:

        [XamlCompilation(XamlCompilationOptions.Compile)]
        public partial class SightPage : ContentPage
        {
            public SightPage ()
            {
                InitializeComponent ();
                PickerBodyType.SelectedIndex = 0;
            }
        }
    
Sign In or Register to comment.