2
votes

I want to get the first data when opening my page but it doesn't. I don't know where I made the mistake. Can someone help?

This my Code;

Xaml

<Picker FontSize="15"
        SelectedItem="{Binding SelectedObject}"
        VerticalOptions="StartAndExpand"
        HorizontalOptions="FillAndExpand"
        ItemsSource="{Binding myList}"
        ItemDisplayBinding="{Binding Title}">

ViewModel


public myViewModel()
{
    GetList();
    SelectedObject = myList[0];
}



public myClass _selectedObject;

public myClass SelectedObject
{
    get
    {
        return _selectedObject;
    }
    set
    {
        _selectedObject= value;
        OnPropertyChanged("SelectedObject");

    }
}

Class

public class myClass
{
   public int ID { get; set; }

   public string Title{ get; set; }

   public string Desc{ get; set; }
}
2
What do you mean by getting first data? Let picker select the first row? Or get the value of first row in Picker? Or something others?Jack Hua
I mean select the first row.YasinGundogdu
Hi do you want the first item display on picker without selecting it?Is that what you mean?Anand
yes, I want to exactly that.YasinGundogdu

2 Answers

3
votes
<Picker.SelectedIndex>0</Picker.SelectedIndex>

or

SelectedIndex="{Binding Index}"

or swap your attributes order

<Picker FontSize="15"
    VerticalOptions="StartAndExpand"
    HorizontalOptions="FillAndExpand"
    ItemsSource="{Binding myList}"
    ItemDisplayBinding="{Binding Title}"
    SelectedItem="{Binding SelectedObject}>
2
votes

I just write a demo and I made it work, you can check it and find out where you have made a mistake:

In code behind:

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();


        myViewModel vm = new myViewModel();
        this.BindingContext = vm;

        //This will also work
        //myPicker.SelectedIndex = 0;
    }
}

public class myViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public List<myClass> myList
    {
        get;
        set;
    }

    public myViewModel()
    {
        GetList();
        SelectedObject = myList[0];
    }

    public void GetList() {

        myList = new List<myClass>();

        myList.Add(new myClass(1,"firstTitle","FirstDesc"));
        myList.Add(new myClass(2, "SecondTitle", "SecondDesc"));
        myList.Add(new myClass(3, "ThirdTitle", "ThirdDesc"));

    }

    public myClass _selectedObject;


    public myClass SelectedObject
    {
        get
        {
            return _selectedObject;
        }
        set
        {
            _selectedObject = value;

            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SelectedObject"));
        }
    }
}

public class myClass
{
    public int ID { get; set; }

    public string Title { get; set; }

    public string Desc { get; set; }

    public myClass(int a , string title , string desc) {

        ID = a;
        Title = title;
        Desc = desc;
    }
}

And in Xaml:

    <!-- Place new controls here -->
<Picker x:Name="myPicker" FontSize="15"
        BackgroundColor="Red"
    VerticalOptions="CenterAndExpand"
    HorizontalOptions="CenterAndExpand"
    ItemsSource="{Binding myList}"
    ItemDisplayBinding="{Binding Title}"
    SelectedItem="{Binding SelectedObject}"
        />

I upload my sample here. Let me know if it works.