Search code examples
c#xamarindata-bindingpicker

Xamarin Picker binding


Am I missing something or is there more to it that I am not getting? I'm working on a mobile app and have to use pickers for choices from a data table. To start, I have many such pickers that are key/value based. I have an internal ID and a corresponding Show value. The IDs do not always have 1, 2, 3 values such as originating from a lookup table and may have things as

KeyID / ShowValue
27 = Another Thing
55 = Many More
12 = Some Item

Retrieved as simple as

select * from LookupTable where Category = 'demo'

So I have this class below that is used for binding the picker via a list of records

public class CboIntKeyValue
{
    public int KeyID { get; set; } = 0;
    public string ShowValue { get; set; } = "";
}

Now, the data record that I am trying to bind to has only the ID column associated to the lookup. Without getting buried into XAML, but in general, I have my ViewModel. On that I have an instance of my data record that has the ID column.

public class MyViewModel : BindableObject
{
    public MyViewModel()
    {
        // Sample to pre-load list of records from data server of KVP
        PickerChoices = GetDataFromServerForDemo( "select * from LookupTable where Category = 'demo'" );


        ShowThisRecord = new MyDataRec();
        // for grins, I am setting the value that SHOULD be defaulted
        // in picker.  In this case, ID = 12 = "Some Item" from above
        ShowThisRecord.MyID = 12;
    }

    // this is the record that has the "ID" column I am trying to bind to
    public MyDataRec ShowThisRecord {get; set;}

    // The picker is bound to this list of possible choices
    public List<CboIntKeyValue> PickerChoices {get; set;}
}

I can’t bind to the index of the list, because that would give me 0, 1, 2, when I would be expecting the corresponding "ID" to be the basis of proper record within the list.

In WPF, I have in the past, been able to declare the show value for the screen, but also the bind value to the ID column in similar. So, the binding of the INT property on my "ShowThisRecord" would drive and properly refresh.

I can see the binding of SelectedItem, but that is the whole item of the KVP class which is not part of the MyDataRec. Only the ID is the common element between them.

What is the proper bindings to get this to work?

<Picker ItemDisplayBinding="{Binding ShowValue}"
   SelectedItem="{Binding ???}" />

Just to confirm my record bindings are legitimate, my page has binding context to MyViewModel as I can properly see the ID via a sample text entry I added to the page via.

<Entry Text="{Binding Path=ShowThisRecord.MyID}"/>

Solution

  • After much work, I ended up writing my own separate class and template style for what I needed. Due to the length of it, and posting the source code for anyone to use, review, assess, whatever, I posted that out on The Code Project.

    Again, the primary issue I had is if I have an integer key ID coming from a data source, the picker would not automatically refresh itself by just the given ID (int or string).