xamarin 选择器不绑定

xamarin picker not binding

我的项目中有多个选择器,但出于某种原因,只有一个选择器没有绑定。我已经弄乱了两天了,无论我换成可观察的集合还是字符串列表,它仍然无法正常工作。

这里是 xaml

                <Picker x:Name="PickerMarket" Title="Market" ClassId="PickerMarket"
                    ItemsSource="{Binding TestList}"
                    ItemDisplayBinding="{Binding ShortDesc}"  
                    SelectedItem="{Binding ShortDesc}"
                    Grid.Row="0" Grid.Column="1" >
            </Picker>

这里是视图模型

class VamiMarketViewModel: INotifyPropertyChanged
{
    private List<Performance> _testList;
    public List<Performance> TestList
    {
        get { return _testList; }
        set
        {
            {
                _testList = value;
                NotifyPropertyChanged();
            }
        }
    }

    private string _shortDesc;
    public string ShortDesc
    {
        get { return _shortDesc; }
        set
        {
            {
                _shortDesc = value;
                NotifyPropertyChanged();
            }
        }
    }

    public class Performance
{
    public int PerformanceDailyTableId { get; set; }
    public DateTime Filedate { get; set; }
    public string Office { get; set; }
    public string Account { get; set; }

    public decimal TradeLevel { get; set; }
    public decimal BeginningEquity { get; set; }
    public decimal fxPL { get; set; }
    public decimal CashActivity { get; set; }
    public decimal CashActivityNonPl { get; set; }
    public decimal TBills { get; set; }
    public decimal OTEChange { get; set; }
    public decimal Realized { get; set; }
    public decimal Commission { get; set; }
    public decimal ClearingFees { get; set; }
    public decimal ExchangeFees { get; set; }
    public decimal NFAFees { get; set; }
    public decimal BrokerageFees { get; set; }
    public decimal TransactionFees { get; set; }
    public decimal NetPerformance { get; set; }
    public decimal EndingEquity { get; set; }
    public decimal DailyROR { get; set; }
    public decimal Vami { get; set; }
    public decimal ADMstmtNLVchange { get; set; }
    public decimal ManualAdjustment { get; set; }
    public decimal DoubleCheck { get; set; }

    public string AccountNumber { get; set; }
    public string Sector { get; set; }
    public string ShortDesc { get; set; }
}

在仅为测试目的创建视图模型时,我试图像这样填充列表

            Performance p1 = new Performance();
        p1.ShortDesc = "user";
        TestList.Add(p1);

        Performance p2 = new Performance();
        p2.ShortDesc = "stephen";
        TestList.Add(p2);

根据我从您的代码中看到的情况,SelectedItem 似乎是问题所在。 由于您的 PickerItemsSource(TestList 属性) 是 List<Performance> 类型,SelectedItem 属性 绑定到 Picker必须是 Performance 类型。但是,在您的情况下,您将其保留为 string 而不是 Performance

ItemDisplayBinding 必须是 Performance object 中任何 属性 的名称,在你的情况下没问题,因为你有一个字符串 属性在你的 Performance class 中调用了 ShortDesc

这就是我在您的代码中看到的问题。像下面这样更改 属性 ShortDesc 的类型,并将 collection TestList 中的任何一项分配给它。您的代码将开始正常工作。

private Performance _shortDesc;
public Performance ShortDesc
{
   get { return _shortDesc; } 
   set
   {
       {
            _shortDesc = value;
            NotifyPropertyChanged();
       }
   }
}

Refer to the documentation here which explains a clear example for Binding objects to Picker.

希望对您有所帮助。