3

I have a property that is of type DateTime that I would like to bind to a viewmodel. When I bind the property I am getting and error stating that the binding type must be a reference type. How can I bind this property to my viewmodel and be notified of it's changes?

Specifically, the code that is causing the compiler error looks like this:

set.Bind (StartDate).To (vm => vm.StartDate);

Here is the property on the view.

public static DateTime StartDate { get; set; }

  • This is a pretty good first question! You may want to include the specific code that is failing to help answerers. You also might consider tagging this with the specific display tech and language you are using, if for nothing else than for some extra visibility (you only have 3 views so far). – BradleyDotNET Jun 13 '14 at 22:42

1 Answers1

3

The general pattern for Fluent binding is:

set.Bind (target).For(v => v.TargetProperty).To (vm => vm.SourceProperty);

This binds the TargetProperty of target to the SourceProperty of the source DataContext (normally the ViewModel).

When For is omitted, then MvvmCross looks up a default property.

In your code, you are trying to bind the default property of the current StartDate to the ViewModel's StartDate. I suspect what you wanted instead was:

set.Bind(this).For(v => v.StartDate).To(vm => vm.StartDate);

For more on fluent data-binding syntax, please see the wiki http://github.com/mvvmcross/mvvmcross/wiki

Stuart
  • 66,722
  • 7
  • 114
  • 165
  • Thank you for responding Stuart. Though this does not work. I get the compiler error "Cannot convert 'lambda expression' to non-delegate type 'string'". – user3630447 Jun 16 '14 at 17:01
  • Can you maybe edit your question to show what the View property is that you are trying to bind? – Stuart Jun 17 '14 at 00:21
  • Sorry, I guess it is easily missed in my description without it being code. I also tried: `set.Bind(this).For("StartDate").To(vm => vm.StartDate);` But it doesn't seem to be firing the setter on the view model. – user3630447 Jun 17 '14 at 18:27
  • 1
    Thanks for editing your question... it makes the problem obvious - you've tried to bind to a `static` property - you can't - you can only bind to instance properties. – Stuart Jun 17 '14 at 18:31
  • That change did get rid of the compiler error. But the setter in the viewmodel still is not firing when the view property is set. – user3630447 Jun 17 '14 at 20:47
  • 1
    For 2-way, take a look at how to do custom bindings - these can be done automatically by convention based naming like in http://stackoverflow.com/questions/23314276/mvvmcross-binding/23318276#23318276 - or can be done using full custom binding code like in the N=28 video. This answer - http://stackoverflow.com/questions/20152826/list-of-localmvxbind-binders/20181703#20181703 - has some good info in it. For your sample, I think automatically using a view based `event` would be easiest. – Stuart Jun 17 '14 at 22:48
  • Do you have a code example of this working? I am unable to get this to work. – user3630447 Jun 18 '14 at 17:29
  • Or possibly pointing me to documentation on the proper implementation of the convention based approach you mentioned. – user3630447 Jun 23 '14 at 16:31
  • Try http://stackoverflow.com/questions/17156368/is-two-way-binding-supported-on-mvvmcross-for-touch-besides-uitextfields/17156634#17156634 ? – Stuart Jun 24 '14 at 03:54
  • Not really sure why, but it's working now. Thank you for your help. – user3630447 Jun 24 '14 at 17:21