title: ReactiveUI 6.0 and WinForms binding
body: <p>Now that ReactiveUI 6.0 <a href="http://log.paulbetts.org/welcome-to-reactiveui-6-0/" rel="nofollow noreferrer">has been released</a>, I have a question to the commuity: what is the best or most efficient way to bind ReactiveObject(s) and Windows Form(s).</p>
  
  <p>Here is what I have so far:</p>
  
  <p><em>My Model</em></p>
  
  <pre><code>namespace WindowsFormsApplication1
  {
      #region
  
      using System;
      using System.Reactive.Linq;
  
      using ReactiveUI;
  
      #endregion
  
      public class ClockModel : ReactiveObject
      {
          #region Fields
  
          private DateTime currentDateTime;
  
          #endregion
  
          #region Constructors and Destructors
  
          public ClockModel() { Observable.Interval(TimeSpan.FromSeconds(1)).Subscribe(s =&gt; this.CurrentDateTime = DateTime.Now); }
  
          #endregion
  
          #region Public Properties
  
          public DateTime CurrentDateTime
          {
              get { return currentDateTime; }
              set { this.RaiseAndSetIfChanged(ref currentDateTime, value); }
          }
  
          #endregion
      }
  }
  </code></pre>
  
  <p><em>My only form</em></p>
  
  <pre><code>using System.Windows.Forms;
  
  namespace WindowsFormsApplication1
  {
      public partial class Form1 : Form
      {
          readonly ClockModel model = new ClockModel();
          public Form1()
          {
              InitializeComponent();
              this.label1.DataBindings.Add("Text", model, "CurrentDateTime");
          }
      }
  }
  </code></pre>
  
  <p>It does work, but I am not thrilled about referring to properties on each side by their string names. Is there a better way to accomplish this?</p>
  
  <p><strong>UPDATE 1</strong></p>
  
  <p>Given <a href="https://stackoverflow.com/questions/301809/workarounds-for-nameof-operator-in-c-typesafe-databinding">this extension</a>:</p>
  
  <pre><code>public class NameOf&lt;T&gt;
  {
      public static string Property&lt;TProp&gt;(Expression&lt;Func&lt;T, TProp&gt;&gt; expression)
      {
          var body = expression.Body as MemberExpression;
          if (body == null)
              throw new ArgumentException("'expression' should be a member expression");
          return body.Member.Name;
      }
  }
  </code></pre>
  
  <p>Is this form any more desirable than the previous one?</p>
  
  <pre><code>this.label1.DataBindings.Add(NameOf&lt;Form1&gt;.Property(e =&gt; e.label1.Text), model, NameOf&lt;ClockModel&gt;.Property(p =&gt; p.CurrentDateTime));
  </code></pre>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>Another extension:</p>
  
  <pre><code>public static class Extensions
  {
      public static string GetPropertyName&lt;T,TProp&gt;(this T obj, Expression&lt;Func&lt;T, TProp&gt;&gt; expression)
      {
          var body = expression.Body as MemberExpression;
          if (body == null)
              throw new ArgumentException("'expression' should be a member expression");
          return body.Member.Name;
      }
  }
  </code></pre>
  
  <p>Now I can use:</p>
  
  <pre><code>this.label1.DataBindings.Add(this.GetPropertyName(e =&gt; e.label1.Text), model, model.GetPropertyName(p =&gt; p.CurrentDateTime));
  </code></pre>
  
  <p>or this:</p>
  
  <pre><code>this.label2.DataBindings.Add(this.label2.GetPropertyName(e =&gt; e.Text), model, model.GetPropertyName(p =&gt; p.Ticks));
  </code></pre>
  
  <p>I start to like this one, but what do others think of any of the three approaches?</p>
  
  <p>Technically speaking, the Form became a View and a Controller or Presenter, while the Model is still separated and has no knowledge of its consumers. Would this be preferred approach?</p>
  
  <p><strong>UPDATE 3</strong></p>
  
  <p>This might cause some purists a mild heart attack ...
  Based on Paul Betts advice:</p>
  
  <pre><code>namespace WindowsFormsApplication1
  {
      #region
  
      using System.Windows.Forms;
  
      using ReactiveUI;
  
      #endregion
  
      public partial class Form1 : Form, IViewFor&lt;ClockModel&gt;
      {
          #region Constructors and Destructors
  
          public Form1()
          {
              InitializeComponent();
              this.ViewModel = new ClockModel();
              this.Bind(ViewModel, vm =&gt; vm.CurrentDateTime, v =&gt; v.label1.Text);
              this.Bind(ViewModel, vm =&gt; vm.Ticks, v =&gt; v.label2.Text);
          }
  
          #endregion
  
          #region Public Properties
  
          public ClockModel ViewModel { get; set; }
  
          #endregion
  
          #region Explicit Interface Properties
  
          object IViewFor.ViewModel
          {
              get { return ViewModel; }
              set { ViewModel = value as ClockModel; }
          }
  
          #endregion
      }
  }
  </code></pre>
  
  <p>Interesting change in behavior: prior to Update 3, time was displayed with seconds. I can only reason that this is due to the default ReactiveUI converter used in the third approach. I have to learn more about it. </p>
  
  <pre><code>public class DateTimeStringConverter : IBindingTypeConverter
  {
      public int GetAffinityForObjects(Type fromType, Type toType) { return 1; }
  
      public bool TryConvert(object @from, Type toType, object conversionHint, out object result) { 
          result = ((DateTime)@from).ToString("F");
          return true;
      }
  }
  </code></pre>
  
  <p>I like the elegance of the approach. </p>
  
  <p>Any other opinions?</p>