title: How can I bind a UITableViewCell's subview event to a view model ICommand?
body: <p>I have a view that uses several MonoTouch.Dialog elements for screen controls. I've been able to create fluent bindings for all of the screen controls except one, a ButtonElement, because I can't access the TouchUpInside event of its UIButton subview.</p>
  
  <p>Here's the pseudocode that creates the controls, builds the screen, and creates the bindings.</p>
  
  <pre><code>// Controls created
  CheckboxElement checkbox = new CheckboxElement("Lorem ipsum dolor");
  StringElement string1 = new StringElement("Sit amet");
  StringElement string2 = new StringElement("Consectetur");
  ButtonElement button = new ButtonElement("Adipiscing", delegate{method();});
  
  // Elements used to create view
  List&lt;Element&gt; elements = new List&lt;Element&gt;(){checkbox, string1, string2, button};
  AddElements(elements);
  
  // Bind controls to view model properties
  var set = this.CreateBindingSet&lt;SettingsView, SettingsViewModel&gt;();
  
  set.Bind(checkbox).For(v =&gt; v.BooleanValue).To(vm =&gt; vm.CheckboxProperty).TwoWay();
  set.Bind(string1).For(v =&gt; v.Value).To(vm =&gt; vm.String1Property);
  set.Bind(string2).For(v =&gt; v.Value).To(vm =&gt; vm.String2Property);
  // Binding to ButtonElement TouchUpInside event would go here
  
  set.Apply();
  </code></pre>
  
  <p>The ButtonElement that has a UIButton as its only subview and it is this subview's TouchUpInside event that I want to link to an ICommand method in the view model.</p>
  
  <pre><code>public ICommand TouchUpInsideCommand()
  {
      get { ... }
  }
  </code></pre>
  
  <p>Right now in ButtonElement, when the UIButton is created in GetCell, its TouchUpInside event is linked to the anonymous delegate method that's passed in as part of ButtonElement's constructor and stored in an NSAction called Tapped.</p>
  
  <pre><code>button.TouchUpInside += (o, e) =&gt; Tapped.Invoke();
  </code></pre>
  
  <p>To remove this event handling I've created a new class called ButtonElementMvx that inherits from ButtonElement. In ButtonElementMvx's GetCell method I remove the event handling that was set up by its parent .</p>
  
  <pre><code>public ButtonElementMvx : ButtonElement
  {
      ...
  
      public override UITableViewCell GetCell(UITableView tv)
      {
          UITableViewCell cell = base.GetCell(tv);
          button.TouchUpInside -= ButtonTouchUpInsideEventHandler;
          return cell;
      }
  }
  </code></pre>
  
  <p>ButtonTouchUpInsideEventHandler is a reference to the anonymous delegate method created in ButtonElement's GetCell (I know this implementation might be a bit kludgy but I'll clean it up later).</p>
  
  <p>Here's my question - What is the best way to create a binding to the ButtonElement's UIButton TouchUpInside event inside my view where I'm creating my other bindings?</p>
  
  <pre><code>buttonElement : ButtonElement
      |
      -&gt; uiButton : UIButton
          |
          -&gt; TouchUpInside
  </code></pre>
  
  <p>Something along the lines of </p>
  
  <pre><code>set.Bind(buttonElement.uiButton).For("TouchUpInside").To(vm =&gt; vm.TouchUpInsideCommand);
  </code></pre>
  
  <p>I've done some research and I think the way forward is to create a custom binding that is added to the registry on startup (outlined in Stuart's <a href="http://slodge.blogspot.co.uk/2013/06/n28-custom-bindings-n1-days-of-mvvmcross.html" rel="nofollow noreferrer">N=28 - Custom Binding</a> video) as well as extending my ButtonElementMvx class to <a href="https://stackoverflow.com/questions/15073262/custom-bindable-control-in-a-mvvmcross-touch-project">create a fully bindable UIButton</a> that I can access the button's TouchUpInside event and bind it in my view along with my other controls.</p>
  
  <p>Any thoughts? Is this the correct way to proceed or am I heading off in the wrong direction?</p>