简体   繁体   English

Xamarin表单-XLabs CustomRadioButton:ItemsSource

[英]Xamarin Forms - XLabs CustomRadioButton : ItemsSource

I'm currently using the CustomRadioButton component from XLabs. 我目前正在使用XLabs的CustomRadioButton组件。 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 . 我从样本中了解到,可以通过为BindableRadioGroup的ItemsSource属性提供字符串数组来设置单选按钮的文本。

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? 如果我有此Person对象的列表,是否可以将其直接提供给BindableRadioGroup的ItemsSource属性? How do I define which attribute will be display on the CustomRadioButton? 如何定义哪个属性将显示在CustomRadioButton上?

I found this method for a ListView but in my case, I can't find the ItemTemplate property for the BindableRadioGroup : 我为ListView找到了此方法,但就我而言,我找不到BindableRadioGroup的ItemTemplate属性:

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. 谢谢。

BindableRadioGroup is actually derived from a StackLayout so I don't think you're going to be able to use a DataTemplate . BindableRadioGroup实际上是从StackLayout派生的,所以我认为您将无法使用DataTemplate

Without having tested this you could try two things. 未经测试,您可以尝试两种方法。 Looking at the code , Text is set to Item.ToString() in OnItemsSourceChanged . 查看代码 ,在OnItemsSourceChanged Text设置为Item.ToString() You could define to a ToString() for your person object. 您可以为您的person对象定义一个ToString()

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 BindableRadioGroup object. 或者,您可以自己处理每个CustomRadioButton的创建,绑定每个属性(如CheckedText然后将每个CustomRadioButton手动添加到BindableRadioGroup对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM