title: ReactiveUI WhenAny behavior dependent on when ReactiveObject is added to an ObservableCollection
body: <p>I am encountering a strange issue and am not sure why it would be occuring. I've nailed it down to the <code>WhenAny</code> or <code>WhenAnyValue</code> methods.</p>
  
  <p>I have two view models with a parent-child relationship. The parent has an <code>ObservableCollection</code> of children. The parent monitors each child's <code>HasError</code> property in order to reevaluate it's own <code>HasErrors</code> property.</p>
  
  <p>Initially, the child is added to the <code>ObservableCollection</code> and its <code>HasErrors</code> property monitored with <code>WhenAnyValue</code>. After this, the <code>HasErrors</code> property is set to <code>true</code> as expected because its <code>Validate</code> method gets called as a result of the <code>WhenAnyValue</code>.</p>
  
  <p>The problem is when the child becomes valid and its <code>HasErrors</code> property gets set to <code>false</code>, the parent does not get notified via <code>WhenAnyValue</code> and thus cannot reevaluate its own <code>HasErrors</code>.</p>
  
  <p>The solution seems to be adding the child to the collection <em>after</em> using <code>WhenAnyValue</code> or using <code>ObservableForProperty</code> instead. It seems strange that this order would matter as the <code>ReactiveObject</code> and <code>ObservableCollection</code> seem independent.</p>
  
  <p>Below is the code that does not work but swapping the last two lines or using <code>ObservableForProperty</code> makes it work:</p>
  
  <pre><code>private void AddChild()
  {
      var child = new ChildViewModel();
      Children.Add(child);
      child.WhenAnyValue(p =&gt; p.HasErrors).Subscribe(p =&gt; Validate());
  }
  </code></pre>
  
  <p>I am using ReactiveUI version 6.2.1.1 but the same behavior is present in version 6.0.1.</p>
  
  <p>Full code example:</p>
  
  <pre><code>public class ParentViewModel : ReactiveObject
  {
      private bool _hasErrors;
  
      public ObservableCollection&lt;ChildViewModel&gt; Children { get; private set; }
  
      public ReactiveCommand&lt;object&gt; AddChildCommand { get; private set; }
  
      public bool HasErrors
      {
          get { return _hasErrors; }
          set { this.RaiseAndSetIfChanged(ref _hasErrors, value); }
      }
  
      public ParentViewModel()
      {
          Children = new ObservableCollection&lt;ChildViewModel&gt;();
          AddChildCommand = ReactiveCommand.Create();
          AddChildCommand.Subscribe(p =&gt; AddChild());
      }
  
      public bool Validate()
      {
          HasErrors = !Children.All(p =&gt; p.Validate());
          return HasErrors;
      }
  
      private void AddChild()
      {
          var child = new ChildViewModel();
          Children.Add(child);
          child.WhenAnyValue(p =&gt; p.HasErrors).Subscribe(p =&gt; Validate());
      }
  }
  
  public class ChildViewModel : ReactiveObject, IDataErrorInfo
  {
      private string _name;
      private bool _hasErrors;
  
      public string Name
      {
          get { return _name; }
          set { this.RaiseAndSetIfChanged(ref _name, value); }
      }
  
      public bool HasErrors
      {
          get { return _hasErrors; }
          set { this.RaiseAndSetIfChanged(ref _hasErrors, value); }
      }
  
      public string Error
      {
          get { return null; }
      }
  
      public string this[string columnName]
      {
          get { return Validate() ? null : "Required"; }
      }
  
      public bool Validate()
      {
          HasErrors = string.IsNullOrWhiteSpace(Name);
          return !HasErrors;
      }
  }
  
  &lt;Window x:Class="MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="MainWindow"
          Height="350"
          Width="525"&gt;
      &lt;StackPanel Margin="5"&gt;
          &lt;StackPanel Orientation="Horizontal"&gt;
              &lt;TextBlock Text="HasErrors: " /&gt;
              &lt;TextBlock Text="{Binding HasErrors}"
                         Margin="3,0,0,0" /&gt;
          &lt;/StackPanel&gt;
          &lt;Button Content="Add"
                  Command="{Binding AddChildCommand}" /&gt;
          &lt;ItemsControl ItemsSource="{Binding Children}"
                        Height="500"&gt;
              &lt;ItemsControl.ItemTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;TextBox Text="{Binding Name, ValidatesOnDataErrors=True}"
                               Width="200" /&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ItemsControl.ItemTemplate&gt;
          &lt;/ItemsControl&gt;
      &lt;/StackPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Edit 1:
  I think I've discovered something that might explain why the issue is occuring. Although I'm not sure why it's the case.</p>
  
  <p>Take the following example:</p>
  
  <pre><code>var child = new ChildViewModel();
  child.WhenAnyValue(p =&gt; p.HasErrors).Subscribe(p =&gt; child.HasErrors = true);
  child.HasErrors = false;
  </code></pre>
  
  <p>When initially setting up the observable using <code>WhenAnyValue</code> the value of <code>HasErrors</code> is <code>false</code>. Then the subscribe gets invoked as part of the initial setup and sets <code>HasErrors</code> to <code>true</code>. However, the <code>WhenAnyValue</code> does not pick up on this change. Therefore, when setting <code>HasErrors</code> to <code>false</code> at a later time, it is not picked up as a change. It seems like there some state maintained that is out of sync when determining if a change actually occurred or not.</p>
  
  <p>Here's another interesting thing:</p>
  
  <pre><code>var child = new ChildViewModel();
  child.WhenAnyValue(p =&gt; p.Name).Subscribe(p =&gt; child.Name = Guid.NewGuid().ToString());
  child.Name = Guid.NewGuid().ToString();
  </code></pre>
  
  <p>For the example above, the stack overflow doesn't happen until line 3. This is another example of where some state seems to be out of sync.</p>