绑定到ListView中的属性

结节状

我有一个由这样的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

有没有一种方法可以根据列表中的属性执行此绑定?

杰森

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

代替

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");

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章