Nodoid
Nodoid

Reputation: 1571

Binding to a property within a ListView

NameI have a List populated by the return values from an SQLite query like this

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

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

On return myClass is non-null.

From this, I try to create a ListView which is where I'm having the binding issue. Currently my code looks like this

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

When I attempt to build, I am getting errors on the SetBinding lines. The error is The best overloaded method match for Xamarin.Forms.BindableObject.SetBinding(Xamarin.Forms.BindableProperty, Xamarin.Forms.BindableBase) has some invalid arguments

Is there a way that I can perform this binding based on the properties within the List?

Upvotes: 0

Views: 961

Answers (1)

Jason
Jason

Reputation: 89102

SetBinding returns a void - assigning it to your property won't do anything useful. You are also trying to bind a Label using TextCell.TextProperty, which is wrong.

Instead of

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

try

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

Upvotes: 2

Related Questions