我遇到了 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/