1

我目前正在使用 XLabs 的CustomRadioButton组件。我从示例中了解到,您可以通过将字符串数组提供给BindableRadioGroup的 ItemsSource 属性来设置单选按钮中的文本。

例子 :

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

现在,我想现在是否可以对一个对象做同样的事情。

例子 :

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

如果我有这个Person对象的列表,我可以将它直接提供给 BindableRadioGroup 的 ItemsSource 属性吗?如何定义将在 CustomRadioButton 上显示的属性?

我为 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" }
  };

谢谢。

4

1 回答 1

2

BindableRadioGroup实际上是从 a 派生的,StackLayout所以我认为您将无法使用 a DataTemplate

如果没有对此进行测试,您可以尝试两件事。看代码Text设置为Item.ToString()in OnItemsSourceChanged。您可以ToString()为您的person对象定义一个。

或者,您可以CustomRadioButton自己处理每个属性的创建,绑定每个属性,例如CheckedText并手动将每个属性添加CustomRadioButton到您的BindableRadioGroup对象中。

于 2016-06-01T09:44:21.883 回答