solarissf
solarissf

Reputation: 1277

xamarin picker not binding

I have multiple pickers in my project and for some reason just one of the pickers are not binding. I've been messing with this for two days now and no matter if I change to observable collections or lists of strings its still not working.

here is the 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>

here is the viewModel

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; }
}

in the creation of the view model just for testing purposes I am trying to populate the list like so

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

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

Upvotes: 2

Views: 1816

Answers (1)

Harikrishnan
Harikrishnan

Reputation: 1474

To what I see from your code, the SelectedItem seems to be the problem. Since your Picker's ItemsSource(TestList property) is of type List<Performance>, the SelectedItem property bound to the Picker must be of type Performance. But, in your case, you have kept it as string instead of Performance.

The ItemDisplayBinding must be the name of any property inside your Performance object which in your case is fine since you have a string property called ShortDesc inside your Performance class.

That's the problem I see in your code. Change the type of the property ShortDesc like below and assign any one of the items in your collection TestList to it. Your code will start working fine.

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.

I hope that helps.

Upvotes: 2

Related Questions