Xamarin Forms - 将 UI 控件绑定到对象属性(这些对象在列表中)

Xamarin Forms - Bind UI controls to object properties (those objects are in a list)

在代码隐藏中,我循环遍历视图模型中定义的列表。对于每个项目,我检查我需要创建什么类型的控件(条目、选择器等)。问题是我不知道如何将这些控件与视图模型中定义的列表中对象的属性绑定..

我的代码:

        // Create controls dynamically
        for (int i = 0; i < (BindingContext as CheckListEditViewModel).CheckListItems.Count; i++)
        {
            var item = (BindingContext as CheckListEditViewModel).CheckListItems[i];

            var description = new Label()
            {
                Text = item.Description
            };
            stack.Children.Add(description);

            if ((item.ChecklistItemType == Domain.ChecklistItemType.Number))
            {
                var numerEntry = new Entry();
                numerEntry.Keyboard = Keyboard.Numeric;

                numerEntry.SetBinding(Entry.TextProperty, "item.Value"); // How should I do this??

                stack.Children.Add(numerEntry);
            }
            else if ((item.ChecklistItemType == Domain.ChecklistItemType.Email))
            {
                var numerEntry = new Entry();
                numerEntry.Keyboard = Keyboard.Email;
                stack.Children.Add(numerEntry);
            }
        }

        Content = stack;

我应该如何将那些 UI 控件与项目对象中的属性绑定? (Ps:项目对象取自列表)

您的 numberEntry 对象不知道对象 item。你不能指望 SetBinding 神奇地知道你对 item 的意思 - 可以是任何东西。为了理解 SetBinding,必须设置控件的 BindingContext。在您的情况下,将其设置为 item

是明智的
var numerEntry = new Entry();
numerEntry.Keyboard = Keyboard.Numeric;

numerEntry.SetBinding(Entry.TextProperty, path: "Value");
numerEntry.BindingContext = item;

stack.Children.Add(numerEntry);

由于 path 参数(为清楚起见而明确编写)表示相对于 BindingContext 属性 的路径,"item.Value" 必须更改为 "Value"

根据你的描述,你循环了List中的item,想把每个item都绑定到UI,我按照你的代码做了一些代码,你可以看看:

 public Page33()
    {
        InitializeComponent();

        this.BindingContext = new CheckListEditViewModel();

        StackLayout stack = new StackLayout();

        for(int i=0;i<(BindingContext as CheckListEditViewModel).CheckListItems.Count;i++)
        {
            var item = (BindingContext as CheckListEditViewModel).CheckListItems[i];

            var description = new Label()
            {
                Text = item.Description
            };
            stack.Children.Add(description);

            if ((item.ChecklistItemType == "Number"))
            {
                var numerEntry = new Entry();
                numerEntry.Keyboard = Keyboard.Numeric;

                numerEntry.SetBinding(Entry.TextProperty, new Binding("value",BindingMode.TwoWay,source:item));

                stack.Children.Add(numerEntry);
            }
            else if ((item.ChecklistItemType == "Email"))
            {
                var numerEntry = new Entry();
                numerEntry.Keyboard = Keyboard.Email;
                numerEntry.SetBinding(Entry.TextProperty, new Binding("value", BindingMode.TwoWay, source: item));
                stack.Children.Add(numerEntry);
            }
        }
        this.Content = stack;
    }
}

public class CheckListEditViewModel:ViewModelBase
{
    private List<EditModel> _CheckListItems;
    public List<EditModel> CheckListItems
    {
        get { return _CheckListItems; }
        set
        {
            _CheckListItems = value;
            RaisePropertyChanged("CheckListItems");
        }
    }
    public CheckListEditViewModel()
    {
        CheckListItems = new List<EditModel>();
        CheckListItems.Add(new EditModel() {Description="one model",ChecklistItemType="Email",value="123@outlook.com" });
        CheckListItems.Add(new EditModel() { Description = "two model", ChecklistItemType = "Number",value="12345" });
        CheckListItems.Add(new EditModel() { Description = "three model", ChecklistItemType = "Email",value="345@outlook.com" });
        CheckListItems.Add(new EditModel() { Description = "four model", ChecklistItemType = "Number",value="567" });
    }
}
public class EditModel
{
    public string Description { get; set; }
    public string ChecklistItemType { get; set; }
    public string value { get; set; }
}

我设置当前的ContentPage BindingContext = new CheckListEditViewModel(),然后循环List中的每一项,然后该项目就是当前控件的来源。