1
votes

I'm using this control in a project and can not get the property binding.

My xaml code:

   <controls:ExtendedPicker x:Name="myPicker" ItemsSource="{Binding ListaLugaresTrabajo}" DisplayProperty="NombreLugar" SelectedItem="{Binding SelectedLugarTrabajo}" />

My ViewModel code:

private ObservableCollection<LugarDeTrabajo> _listaLugaresTrabajo;

    public ObservableCollection<LugarDeTrabajo> ListaLugaresTrabajo {
        get {
            return _listaLugaresTrabajo;
        }
        set {
            _listaLugaresTrabajo = value;
            RaisePropertyChanged (() => ListaLugaresTrabajo);
        }
    }

    private LugarDeTrabajo _selectedLugarTrabajo;

    public LugarDeTrabajo SelectedLugarTrabajo {
        get {
            return _selectedLugarTrabajo;
        }
        set {
            _selectedLugarTrabajo = value;
            RaisePropertyChanged (() => SelectedLugarTrabajo);
        }
    }

My model code (also uses sqlite):

[Table ("LugaresDeTrabajo")]
public class LugarDeTrabajo
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }

    [Unique]
    public string NombreLugar { get; set; }
}

ItemsSource and DisplayProperty works fine but SelectedItem is always null.

I use MvvmCross framework, in the rest of the application works correctly.

It is a shared project and I am trying out the Android version.

That could be happening?

Solution: Set SelectedItem Property to TwoWay binding mode.

Correct Xaml code:

<controls:ExtendedPicker x:Name="myPicker" ItemsSource="{Binding ListaLugaresTrabajo}" DisplayProperty="NombreLugar" SelectedItem="{Binding SelectedLugarTrabajo, Mode=TwoWay}" />
2

2 Answers

3
votes

According to ExtendedPicker source code SelectedItem bindable property has as default binding mode set to OneWay. That means changes in ViewModel are reflected in View but not in the opposite direction.

If you need to propagate your changes from ViewModel to View and vice versa then set SelectedItem property in following way:

SelectedItem="{Binding SelectedLugarTrabajo, Mode=TwoWay}" 

If you want to propagate changes only from View to ViewModel set SelectedItem in following way:

SelectedItem="{Binding SelectedLugarTrabajo, Mode=OneWayToSource}" 
0
votes

you can also rely on the SelectedIndexChanged property. Here is an example:

<controls:ExtendedPicker x:Name="myPicker" ItemsSource="{Binding ListaLugaresTrabajo}" DisplayProperty="NombreLugar"  SelectedIndexChanged="OnLugarChange"/>

    async void OnLugarChange(object sender, System.EventArgs e)
    {
        if (myPicker.SelectedIndex != -1)
        {
            _selectedLugarTrabajo = myPicker.SelectedItem as LugarDeTrabajo;
        }
     }