title: MvxRadioGroupSelectedIndexBinding
body: <p>i created a RadioGroupSelectedIndexBinding from the source of MvxRadioGroupSelectedItemBinding.</p>
  
  <p>It works ok, but not at viewmodel startup: the binding from viewmodel to view is called but at this time the RadioGroup has no child views. It seems they haven't been inflated yet.</p>
  
  <p>This is a "bug" (or feature :p) in Mvvmcross custom inflater/binding ? Or is there something to overload in MvxAndroidTargetBinding ?</p>
  
  <p>Edit: code of MvxRadioGroupSelectedIndexBinding (Index, not Item: different from MvxRadioGroupSelectedItemBinding).</p>
  
  <pre><code>public class MvxRadioGroupSelectedIndexBinding : MvxAndroidTargetBinding
  {
      bool stopListeningCheckChanged = false;
  
      private int selectedIndex = -2;
      public int SelectedIndex
      { 
          get { return selectedIndex; }
          set { if(value != selectedIndex) { selectedIndex = value; FireValueChanged(SelectedIndex); } }
      }
  
      public static void Register(IMvxTargetBindingFactoryRegistry registry)
      {
          registry.RegisterCustomBindingFactory&lt;RadioGroup&gt;("SelectedIndex", radioGroup =&gt; new MvxRadioGroupSelectedIndexBinding(radioGroup));
      }
  
      public MvxRadioGroupSelectedIndexBinding(RadioGroup radioGroup) : base(radioGroup)
      {
          if (radioGroup == null)
          {
              Mvx.Trace(MvxTraceLevel.Error, "RadioGroup SelectedIndex: radioGroup is null");
              return;
          }
  
          radioGroup.CheckedChange += CheckedChange;
          radioGroup.ChildViewAdded += RadioGroupOnChildViewAdded;
      }
  
      private void RadioGroupOnChildViewAdded(object sender, ViewGroup.ChildViewAddedEventArgs childViewAddedEventArgs)
      {
          var radioGroup = Target as RadioGroup;
          if (selectedIndex == radioGroup.ChildCount-1)
          {
              stopListeningCheckChanged = true;
              radioGroup.Check(radioGroup.GetChildAt(selectedIndex).Id);
              stopListeningCheckChanged = false;
          }
      }
  
      private void CheckedChange(object sender, RadioGroup.CheckedChangeEventArgs e)
      {
          if (stopListeningCheckChanged)
              return;
  
          var radioGroup = Target as RadioGroup;
          var checkedId = e.CheckedId;
  
          if (checkedId == View.NoId)
          {
              SelectedIndex = -1;
              return;
          }
  
          for (var i = radioGroup.ChildCount - 1; i &gt;= 0; i--)
          {
              if (checkedId == radioGroup.GetChildAt(i).Id)
              {
                  SelectedIndex = i;
                  return;
              }
          }
          SelectedIndex = -1;
          Mvx.Trace(MvxTraceLevel.Error, "RadioGroup id not found: {0}", checkedId);
      }
  
      public override void SetValue(object index)
      {
          var radioGroup = Target as RadioGroup;
          if (radioGroup == null)
              return;
  
          stopListeningCheckChanged = true;
          selectedIndex = (int)index;
          if (selectedIndex &lt; 0 || selectedIndex &gt;= radioGroup.ChildCount)
          {
              radioGroup.ClearCheck();
          }
          else
          {
              radioGroup.Check(radioGroup.GetChildAt(selectedIndex).Id);
          }
          stopListeningCheckChanged = false;
      }
  
      public override Type TargetType
      {
          get { return typeof(object); }
      }
  
      protected override void SetValueImpl(object target, object value)
      {
      }
  
      public override MvxBindingMode DefaultMode
      {
          get { return MvxBindingMode.TwoWay; }
      }
  
      protected override void Dispose(bool isDisposing)
      {
          if (isDisposing)
          {
              var radioGroup = Target as RadioGroup;
              if (radioGroup != null)
              {
                  radioGroup.CheckedChange -= CheckedChange;
                  radioGroup.ChildViewAdded -= RadioGroupOnChildViewAdded;
              }
          }
          base.Dispose(isDisposing);
      }
  }
  </code></pre>
  
  <p>And usage</p>
  
  <pre><code>    &lt;RadioGroup
          android:orientation="horizontal"
          local:MvxBind="SelectedIndex SelectedChoiceIndex"&gt;
          &lt;RadioButton
              android:text="choice 1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" /&gt;
          &lt;RadioButton
              android:text="choice 2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" /&gt;
      &lt;/RadioGroup&gt;
  </code></pre>
  
  <p>The "bug" is that when SetValue is called, the RadioGroup has no childs. I suppose the custom inflater create and binds views at the same time. In fact it should bind views only when their childs are inflated. I may be wrong though, i've not checked the source code. And it could have other bad side effects.</p>