绑定到ListView中的属性

时间:2014-12-22 13:12:53

标签: listview xamarin.forms

NameI有一个List,填充来自SQLite查询的返回值,如

class TableClass
{
    public string Name {get;set};
    public string id {get;set};
}

List<TableClass> myClass = Database.GetListOfObjects<TableClass>();

返回时myClass为非null。

由此,我尝试创建一个ListView,这是我遇到绑定问题的地方。目前我的代码看起来像这样

var publist = new ListView
            {
                ItemsSource = pubgroups,
                IsGroupingEnabled = true,
                GroupDisplayBinding = new Binding("Public"),
                ItemTemplate = new DataTemplate(() =>
                    {
                        var label = new Label()
                        {
                            Text = SetBinding(TextCell.TextProperty, "Name"),
                            ClassId = SetBinding(ClassIdProperty, "id")
                        };
                        var image = new Image()
                        {
                            ClassId = SetBinding(ClassIdProperty, "id"),
                            Source = Device.OS == TargetPlatform.WinPhone ? "Images/groups" : "groups",
                            WidthRequest = 50,
                            HeightRequest = 50,
                        };
                        return new ViewCell
                        {
                            View = new StackLayout
                            {
                                Padding = new Thickness(0, 5),
                                Orientation = StackOrientation.Horizontal,
                                Children =
                                {
                                    image,
                                    new StackLayout
                                    {
                                        VerticalOptions = LayoutOptions.Center,
                                        Spacing = 0,
                                        Children =
                                        {
                                            label
                                        }
                                    }
                                }
                            }
                        };
                    }),
            };
            pubgroupstack.Children.Add(publist);

当我尝试构建时,我在SetBinding行上遇到错误。错误是 The best overloaded method match for Xamarin.Forms.BindableObject.SetBinding(Xamarin.Forms.BindableProperty, Xamarin.Forms.BindableBase) has some invalid arguments

有没有办法可以根据List中的属性执行此绑定?

1 个答案:

答案 0 :(得分:2)

SetBinding返回一个void - 将它分配给你的属性不会做任何有用的事情。您还尝试使用TextCell.TextProperty绑定Label,这是错误的。

而不是

var label = new Label()
    {
        Text = SetBinding(TextCell.TextProperty, "Name"),
        ClassId = SetBinding(ClassIdProperty, "id")
    };

var label = new Label();
label.SetBinding(Label.TextProperty, "Name");
label.SetBinding(Label.ClassIdProperty, "id");