1
votes

I have a Picker bounded to a ObservableCollection object. The items are populated from a web service (json data).

The picker displays the data correctly, but the SelectedItem doesn't work.

Here is my xaml code:

  <Picker
        ItemsSource="{Binding RatesTax}"
        ItemDisplayBinding="{Binding Code}"
        SelectedItem="{Binding SourceRate, Mode=TwoWay}">

    </Picker>

RatesTax is an object of RatesView class

 public class RatesView 
{
    public string Code
    {
        get; set;
    }

    public double TaxRate
    {
        get; set;
    }      
}

and this is the propertie

 public ObservableCollection<RatesView> RatesTax { get; set; }

this is the sourceRate atribute

private RatesView sourceRate;

and the SourceRate properties

   public RatesView SourceRate
    {
        set
        {
            if (sourceRate != value)
            {
                sourceRate = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SourceRate"));
            }
        }
        get
        {
            return sourceRate;
        }
    }

I put a Break Point in SourceRate to see if it enters when I pick some data of the picker but it does not enter in the SourceRate procedure. I thinks SelectedItem="{Binding SourceRate} is not working as expected.

thak you for your time!

2
the types of SelectedItem and ItemsSource do not matchJason
thank you for your answer! so SourceRate new to be a RatesTax object?Armando Alvarez

2 Answers

1
votes

The SelectedItem property must be of the same type as your items in the collection. So in your case, the SourceRate property and it's backing field sourceRate must be of type RatesView.

Also make sure that your binding is two-way by setting Mode=TwoWay in your binding:

<Picker
    ItemsSource="{Binding RatesTax}"
    ItemDisplayBinding="{Binding Code}"
    SelectedItem="{Binding SourceRate, Mode=TwoWay}">
</Picker>
1
votes

It might be a bug on Xamarin side. If you make your SourceRate a string, you will get your selected Code when picked. It does not route the bound object for some reason, but only Display value. It does not seem a desired/intended behavior.