我遇到了与xamarin表单实验室(Xamarin-forms-labs)复选框控件的绑定问题 . 我有一个listview指向一个联系人数据源(这是一个可观察的集合) . 在列表视图中,我有一个自定义视图单元"InviteItemCell"(参见下面的代码) .
绑定似乎不是双向工作,即它在读取数据源时正确绑定并指示选择了哪些联系人,但是当通过检查UI中的复选框选择联系人时,底层联系对象属性不会改变 .
这是listview的定义:
var stack = new StackLayout ();
list.ItemsSource = App.Service.Contacts;
list.ItemTemplate = new DataTemplate (typeof(InviteItemCell));
这是自定义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 ();
}
}
1 回答
试试这个: