如何使用对象绑定项目源?

时间:2019-01-19 09:31:57

标签: class xaml xamarin

我无法绑定。 并显示数据对象,例如MyProject.Models.Class

`XAML
<Picker ItemsSource="{Binding City}" x:Name="selectCity" />`
`C#`
`List<AddInfo> addInfo = new List<AddInfo>`
      `  {
            new Class{City ="West Amman" },
            new Class{City ="East Amman" },
            new Class{City ="Ajloun" }
        };`  
`MyProject.Model.Class`    
`public class Class{
public string City { get; set; }
}` 
`Output` 
`MyProject.Model.Class
MyProject.Model.Class
MyProject.Model.Class`

1 个答案:

答案 0 :(得分:0)

XAML

<Picker x:Name="picker" Title="Select Country" ItemDisplayBinding="{Binding Name}" >
</Picker>

在您的视图模型中使用它

C#

public partial class SamplePage: ContentPage
{
    public ObservableCollection<Country> CountryList { get; set; } = new ObservableCollection<Country>
    {
        new Country{Name = "India" },
        new Country{Name = "Australia" },
        new Country{Name = "UAE" },
        new Country{Name = "USA" },
    };

    public SamplePage()
    {
        InitializeComponent();
        picker.ItemsSource = CountryList;
    }
}

国家班级

public class Country
{
    public string Name { get; set; }
}