-1

I have a custom control, override of UIScrollView. When rendering two UIViews are rendered. One is the PDF, one is a layer above. The layer above contains a collection of texts which rendered at different places over the pdf.

I use MvvmCross so have a model. Collection of texts is observable. How can I bind the observable collection to the results which need to be rendered in the layer?

In short...pseudo

UIScrollViewWrapper
On draw create two layers
layer 1 is pdf image
layer above is view with texts.

Texts need to be bind and observed by Model.Texts (ObservableCollection<string>)

I tried:

var set = this.CreateBindingSet<ViewWithScrollView, ViewWithScrollViewModel>();
set.Bind(ScrollView.LayerView).For(x => x.LayerItems).To(vm => vm.LayerItems);
set.Apply();

The LayerView is MvxView for bindingcontext

eL-Prova
  • 1,084
  • 11
  • 27
  • Your question is not clear . And could you share more code what you have tried? – ColeX Sep 05 '17 at 08:46
  • I have found a working solution which I will post, I will also try to make my question more clear with a simple image. Update will follow soon! Ps. for the downvoter, just write a comment like Cole instead of downvote alone... – eL-Prova Sep 05 '17 at 10:01

1 Answers1

2

Ok the solution

The own implementation of the scrollview will be:

public class MyOwnScrollView : UIScrollView, IUIScrollViewDelegate, IMvxBindable
{
   //Implement the IMvxBindable
   //Add property BindingContext
   public IMvxBindingContext BindingContext { get; set; }

   //Property for holding the datacontext
   [MvxSetToNullAfterBinding]
   public object DataContext
   {
       get { return BindingContext.DataContext; }
       set { BindingContext.DataContext = value; }
   }

   public MyOwnScrollView()
   {
        //Call Mvx for creating the bindingcontext
        this.CreateBindingContext()
   }

   //Create something where you can create the set voor fluent binding with the view model. For example
   public void MyBindingToCreate()
   {
        var set = new new MvxFluentBindingDescriptionSet<AViewInMyScrollView, MyViewViewModel>(AViewInMyScrollView); //Because CreateBindingSet is part of MvxView and UIScrollView is not MvxView
        set.Bind(AViewInMyScrollView).For(x => x.MyPropertyOfView).To(vm => vm.PropertyOfViewModel);
        set.Apply();
}

And the place where you are using the ScrollView will be a binding to the datacontext

public partial class MyView : MvxView
{
    public MyView() : base("MyView", null)
    {

    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        var set = this.CreateBindingSet<MyView, MyViewViewModel>();
        set.Bind(MyOwnScrollView).For(s => s.DataContext).To(vm => vm);
        set.Apply();
    }
}

PS. I will implement this in MvvmCross by own control. However, this can be used for all own controls!

eL-Prova
  • 1,084
  • 11
  • 27