简体   繁体   中英

xamarin forms labs binding issue with listview and checkbox

I am experiencing binding issues with xamarin forms lab ( Xamarin-forms-labs ) checkbox control. I have a listview which points to a contacts datasource (this is an observable collection). In the list view I have a custom view cell "InviteItemCell" (see code below).

The binding does not appear to be working both ways ie it binds correctly when reading the datasource and indicating which contacts are selected, however when selecting a contact by checking the checkbox through the UI, the underlying contact object property does not change.

here is the definition of the listview:

var stack = new StackLayout ();
list.ItemsSource = App.Service.Contacts;
list.ItemTemplate = new DataTemplate (typeof(InviteItemCell));

Here is the custom viewcell:

public class InviteItemCell : ViewCell
    {
        public InviteItemCell ()
        {
            var chkInvite = new CheckBox ()
            { 
                TextColor = Color.White
            };

            chkInvite.SetBinding (CheckBox.DefaultTextProperty, "FullName");
            chkInvite.SetBinding (CheckBox.CheckedProperty, "Selected");

            var layout = new StackLayout 
            {
                Padding = new Thickness(20, 0, 0, 0),
                Orientation = StackOrientation.Horizontal,
                HorizontalOptions = LayoutOptions.StartAndExpand,
                Children = {chkInvite}
            };

            View = layout;
        }

        protected override void OnBindingContextChanged ()
        {
            View.BindingContext = BindingContext;
            base.OnBindingContextChanged ();
        }
    }

Try this:

chkInvite.SetBinding (CheckBox.DefaultTextProperty, "FullName", BindingMode.TwoWay);
chkInvite.SetBinding (CheckBox.CheckedProperty, "Selected", BindingMode.TwoWay);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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