1

This probably sounds really stupid, but I just can't get a binding to an instance variable (of type string) to work.

In my view's xib, I create a text field as an outlet in IB, then I can bind it to my viewModel's string property. However, it won't let me bind my view's string variable to the viewModel's property in the same way.

Does anyone know if this is by design, or am I missing something? The binding code is -

this.AddBindings(
     new Dictionary<object, string>()
     {
          { TextFieldTemp, "{'Text':{'Path':'AboutText'}}" },
     });
SomaMan
  • 4,127
  • 1
  • 34
  • 45
  • What do you mean by "However, it won't let me bind my view's string variable to the viewModel's property in the same way." - what is your "view's string variable"? I guess I am asking "what Is TextFieldTemp?" – Stuart Jun 07 '12 at 11:09
  • @Stuart - TextFieldTemp is a UITextField I added in IB, which I've bound to the ViewModel's property. However, I'd prefer to bind to one of the View's instance variables as I'd like to do stuff to the value before displaying it. – SomaMan Jun 07 '12 at 11:19
  • I've answered... in response to this comment - you could also consider using a ValueConverter for the "do stuff" - or you could also consider just handcrafting an event handler - I'll add that to the answer... – Stuart Jun 07 '12 at 11:22

1 Answers1

2

From reading your question, I think what you are saying is that the View itself has a field of type string...

Your code:

this.AddBindings(
     new Dictionary<object, string>()
     {
          { StringTemp, "{'Text':{'Path':'AboutText'}}" },
     });

is trying to bind the property Text on the object referred to by StringTemp to whatever is in AboutText on the ViewModel.


To set the StringTemp string itself you should be able to bind to it using something like:

 this.AddBindings(
      new Dictionary<object, string>()
      {
           { this, "{'StringTemp':{'Path':'AboutText'}}" },
      });

Just to explain the parts in: { this, "{'StringTemp':{'Path':'AboutText'}}" }, these can be thought of as { TargetObject, "{'TargetPropertyName':{'Path':'SourcePropertyName'}}" } where:

  • TargetObject (this) is the object you are aiming to set property values on
  • TargetPropertyName (StringTemp) is the name property that you are aiming to set
  • SourcePropertyName (AboutText) is the name of the property that will be the source of the value

Note that Mvx uses Properties - not fields - so private string StringTemp {get;set;} is bindable, but private string StringTemp; is not.


You could also do two-way binding for this string reference if you wanted to... but you would need to setup some custom binding information to do so - there would need to be some event fired and captured in order to update the ViewModel (I'll leave that for another day!)


For situations where direct binding isn't what you are looking for, then you can always subscribe to PropertyChanged and handle the notifications in more verbose code... e.g.:

ViewModel.PropertyChanged += (s,e) => 
{
    if (e.PropertyName == "AboutText")
    {
         // do something complicated here with the new ViewModel.AboutText value
    }
};

...but I personally tend to avoid this type of code where I can...

Stuart
  • 66,722
  • 7
  • 114
  • 165
  • Thanks! That worked perfectly (I don't need 2-way) - we're a bit new to MVVMCross here, so I'm hoping I won't be inundating you with questions... Thanks also for the PropertyChanged bit, I can see how that could be useful on a couple of occasions. – SomaMan Jun 07 '12 at 11:37
  • Forgot to add - the reason I can't use a converter is I'm getting the HTML for a UIWebView, which I need to tell to load its data. – SomaMan Jun 07 '12 at 11:51
  • Cool - if you ever need custom bindings then the code is a bit like the droid stuff - see http://stackoverflow.com/questions/10700445/in-mvvmcross-how-do-i-do-custom-bind-properties - thanks for using StackOverflow and GitHub issues - definitely my preference :) For short questions you can also try http://jabbr.net/#/rooms/mvvmcross - but I'm literally about to leave the country for the weekend :) – Stuart Jun 07 '12 at 12:02