android - xamarin 表单与 ListView 和复选框的实验室绑定(bind)问题

标签 android xamarin xamarin.forms

我遇到了 xamarin 表单实验室 ( Xamarin-forms-labs) 复选框控件的绑定(bind)问题。我有一个 ListView 指向联系人数据源(这是一个可观察的集合)。在 ListView 中我有 自定义 View 单元格“InviteItemCell”(参见下面的代码)。

绑定(bind)似乎不是双向工作的,即它在读取时正确绑定(bind) 数据源并指示选择了哪些联系人,但是在选择联系人时 通过 UI 选中复选框,底层联系人对象属性不会更改。

这里是 ListView 的定义:

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

这是自定义视单元:

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

最佳答案

试试这个:

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

关于android - xamarin 表单与 ListView 和复选框的实验室绑定(bind)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26483994/

相关文章:

android - 在安卓中使用jdbc

android - 后台服务器方式

ios - 命名空间中不存在类型或命名空间名称 "MyBindingLib"(您是否缺少程序集引用?)

c# - 如何在 Zebra MC92N0 上以编程方式发出蜂鸣声?

c# - 无法添加在 Xamarin PCL 项目中使用的程序集 System.Collections.Concurrent

android - 我什么时候应该删除临时图像文件?

c# - XML 中的 Xamarin Android 比例位图

c# - 弃用绑定(bind),新的方法是什么?

c# - Xamarin 签名板。无法保存签名

android - 过滤 ACTION_SEND 共享选项