AlexUT
AlexUT

Reputation: 155

Binding sub properties MvvmCross

Does mvvmcross natively support binding to nested properties?

For example I have a view model as follows:

class MainViewModel : MvxViewModel
{
    public SubViewModelBase SubViewModel 
    { 
           get { return _subViewModel; }
           set { _subViewModel = value; RaisePropertyChanged(() => SubViewModel); }
    }
}

The sub view model may change, but the MainView will bind to the same properties for ALL SubViewModelBase classes... an example SubViewModelBase class as follows:

class SubViewModelBase : MvxViewModel
{
    public bool ShowIndeterminantProgress
    {
          get { return _showIndeterminantProgress; }
          set { _showIndeterminantProgress = value;  RaisePropertyChanged(() => ShowIndeterminantProgress);}
    }

    // ... More common properties ...
}

So the MainView would ideally bind like this

          this.CreateBinding(_progressBar)
         .For(view=> view.Visibility)
         .To<MainViewModel>(vm => vm.SubViewModel.ShowIndeterminantProgress)
         .WithConversion("Visibility")
         .Apply();

Should this work? It doesn't appear to be working, but there are no binding errors in the output?

Upvotes: 1

Views: 2854

Answers (1)

Stuart
Stuart

Reputation: 66882

Does mvvmcross natively support binding to nested properties?

Yes

Should this work?

Yes

For example, this line in ApiExamples for Xamarin.iOS is working:

        set.Bind(errorLabel2).To(vm => vm.Errors.Count);

https://github.com/MvvmCross/MvvmCross-Tutorials/blob/master/ApiExamples/ApiExamples.Touch/Views/FirstView.cs#L361

The set of supported functionality is described in https://github.com/MvvmCross/MvvmCross/wiki/Databinding#fluent - but admittedly this fluent binding is more established/used in Xamarin.iOS than it is in Wpf.


To try to debug why your current binding might not be working try adding a simple property to your view which provides debug output

private Visibility _mockVisibility;
public Visibility MockVisibility
{
   get
   {
      return _mockVisibility;
   }
   set
   {
       Debug.WriteLine("MockVisibility called with " + value);
       _mockVisibility = value;
   }
}

and binding this as:

  this.CreateBinding(this)
     .For(view=> view.MockVisibility)
     .To<MainViewModel>(vm => vm.SubViewModel.ShowIndeterminantProgress)
     .WithConversion("Visibility")
     .Apply();

This should give you debug/trace output and should give you somewhere to put a breakpoint to understand the call stack a little too (although expect this to contain lots of reflection which can be hard to read through).

Beyond this:

Upvotes: 3

Related Questions