简体   繁体   English

Xamarin形成带有ListView和复选框的Labs绑定问题

[英]xamarin forms labs binding issue with listview and checkbox

I am experiencing binding issues with xamarin forms lab ( Xamarin-forms-labs ) checkbox control. 我在使用xamarin表单实验室( Xamarin-forms-labs )复选框控件时遇到绑定问题。 I have a listview which points to a contacts datasource (this is an observable collection). 我有一个listview指向一个联系人数据源(这是一个可观察的集合)。 In the list view I have a custom view cell "InviteItemCell" (see code below). 在列表视图中,我有一个自定义视图单元格“ InviteItemCell”(请参见下面的代码)。

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. 绑定似乎不能同时起作用,即,在读取数据源并指示选择了哪些联系人时,它可以正确绑定,但是,通过通过UI选中复选框来选择联系人时,底层的联系人对象属性不会更改。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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