Rodney
Rodney

Reputation: 23

Xamarin Forms - XLabs CustomRadioButton : ItemsSource

I'm currently using the CustomRadioButton component from XLabs. I learned from the sample that you can set the text from the radio buttons by giving a string array to the ItemsSource property of the BindableRadioGroup.

Example :

BindableRadioGroup buttonGroup = new BindableRadioGroup();
ansPicker.ItemsSource = new[]
        {
            "Red",
            "Blue",
            "Green",
            "Yellow",
            "Orange"
        };

Now, I would like to now if it is possible to do the same thing with an object.

Example :

public class Person {
    private string name;
    private int age;
}

If I have a List of this Person object, can I give it directly to the ItemsSource property of the BindableRadioGroup? How do I define which attribute will be display on the CustomRadioButton?

I found this method for a ListView but in my case, I can't find the ItemTemplate property for the BindableRadioGroup :

var template = new DataTemplate (typeof (TextCell));
// We can set data bindings to our supplied objects.
  template.SetBinding (TextCell.TextProperty, "FullName");
  template.SetBinding (TextCell.DetailProperty, "Address");

  itemsView.ItemTemplate = template;
  itemsView.ItemsSource = new[] {
    new Person { FullName = "James Smith", Address = "404 Nowhere Street" },
    new Person { FullName = "John Doe", Address = "404 Nowhere Ave" }
  };

Thanks.

Upvotes: 1

Views: 1881

Answers (1)

AllDayer
AllDayer

Reputation: 993

BindableRadioGroup is actually derived from a StackLayout so I don't think you're going to be able to use a DataTemplate.

Without having tested this you could try two things. Looking at the code, Text is set to Item.ToString()in OnItemsSourceChanged. You could define to a ToString() for your person object.

Alternatively you could handle the creation of each CustomRadioButton yourself, binding each property such as Checked and Text and manually add each CustomRadioButton to your BindableRadioGroupobject.

Upvotes: 2

Related Questions