Binding a Custom View In Xamarin.Forms

后端 未结 2 437
余生分开走
余生分开走 2020-12-16 16:31

I have a problem binding data in a custom view in Xamarin forms to the view model of the containing page.

My Custom View is very simple, a pair of labels representin

2条回答
  •  鱼传尺愫
    2020-12-16 16:36

    I was able to get this to work by doing

    public KeyValueView()
    {
        InitializeComponent();
        this.VerticalOptions = LayoutOptions.Start;
        this.Content.BindingContext = this;
    }
    

    and then doing this in the xaml.cs for the view you're using the custom view in:

    public ParentView()
    {
        InitializeComponent();
        BindingContext = this;
    }
    

    For an Entry field, which gave me problems, I had to make sure the defaultBindingMode parameter was set to TwoWay.

提交回复
热议问题