Home > Back-end >  Xamarin How to bind a class property (from another project) to the Picker.ItemDisplayBinding propert
Xamarin How to bind a class property (from another project) to the Picker.ItemDisplayBinding propert

Time:10-29

I am trying to populate and display a List"magasin" inside a picker.

The magasin class come from another project, added to the dependencies of my main project.

I am able to populate the picker, but I didn't manage to set Picker.ItemDisplayBinding = new Binding("otherProject.magasin.Name"); , it's just displaying otherProject.magasin for each items.

Here my code :

using otherProject; 

public partial class fForm: ContentPage
{
    public List<otherProject.magasin> listeMagasin;
    public fForm()
    {
        BindingContext = this;
        InitializeComponent();

        listeMagasin = otherProject.magasin.chargerMagasins();

        if (listeMagasin != null)
        {
            pickerMagasin.ItemsSource = listeMagasin;

            pickerMagasin.ItemDisplayBinding = new 
                Binding("otherProject.magasin.Name");
        }
       
    }
}

And my XAML

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="mainProject.fForm">
<ContentPage.Content>
   <StackLayout HorizontalOptions="FillAndExpand">
            <Picker x:Name="pickerMagasin"></Picker>
        </StackLayout>
</ContentPage.Content>

And magasin class

namespace otherProject
{
   public class magasin
   {
    public string Code;
    public string Name;

    public magasin(string code)
    {
        Code = code;
    }

    public static List<magasin> chargerMagasins(){
    //Code here}
   }
}

I tried Binding("Name") or Binding("magasin.Name"), but no success

Thanks for your help !

CodePudding user response:

I think it is because Name and Code are just fields, they should be a property. Try to change magasin class like

public string Name {get;set;} 
public string Code {get;set;} 

Then Binding("Name") will work.

  • Related