我对xaml和C#数据绑定还很陌生。我正在尝试使用ItemsSource,ItemDisplayBinding和SelectedItem将Picker绑定到IList。选取器绑定到IList,但是没有显示正确的信息。
我查看了一些资源,Monkey App选择器是一个很大的帮助。 https://github.com/xamarin/xamarin-forms-samples/tree/master/UserInterface/MonkeyAppPicker
我试图复制他的所作所为,但无济于事。
<Picker ItemsSource="{Binding Mills}"
ItemDisplayBinding="{Binding Name}"
SelectedItem="{Binding SelectedMill, Mode=TwoWay}"/>
namespace TDSDesktop
{
public class PurchaseAgreementBindings : INotifyPropertyChanged
{
public class Mill
{
public string Name { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Phone { get; set; }
}
public static class MillData
{
public static IList<Mill> Mills { get; private set; }
static MillData()
{
Mills = new List<Mill>();
Mills.Add(new Mill
{
Name = "Name1",
Address1 = "123",
Address2 = "City, State Zip",
Phone = "555-555-5555"
});
Mills.Add(new Mill
{
Name = "Name2",
Address1 = "456",
Address2 = "City, State Zip",
Phone = "888-888-8888"
});
}
}
public IList<Mill> Mills { get { return MillData.Mills; } }
Mill selectedMill;
public Mill SelectedMill
{
get { return selectedMill; }
set
{
if (selectedMill != value)
{
selectedMill = value;
OnPropertyChanged();
}
}
}
}
}
理论上,选择器应显示两个值(Name1和Name2),因为我将ItemDisplayBinding设置为Name。但是,当我运行程序时,两个值均为“ TDSDesktop.PurchaseAgreementBindings + Mill”。显然我缺少了一些东西。如果有人可以解释我该如何解决此问题,我将不胜感激。