title: WPF lost Databinding
body: <p>I'm new to WPF and its Databinding, but I stumbled upon a strange behaviour I could not resolve for myself.</p>
  
  <p>In a Dialog I've got a Listbox with Users and a TextBox for a username. Both are bound to a UserLogonLogic-which publishes among others a <code>CurrentUser</code> property.</p>
  
  <p>I want the TextBox to update its text when I click on a name in the ListBox. I also want the <code>SelectedItem</code> in the ListBox to be updated when I enter a username directly into the TextBox. Partial names in the TextBox will be resolved to the first matching value in the listbox or null if there is none.</p>
  
  <p>At first the TextBox gets updated every time I click into the ListBox. Debug shows me that every time the <code>PropertyChangeEvent</code> for <code>CurrentUser</code> is fired the method <code>txtName_TextChanged</code> method is called. Only after I have typed something into the textbox the <code>DataBinding</code> of the TextBox seems to be lost. There will be no further updates of the TextBox when I click into the ListBox. Debug now shows me that the method <code>txtName_TextChanged</code> is no longer being called after the <code>CurrentUser</code> <code>PropertyChangeEvent</code> is fired.</p>
  
  <p>Does anybody have an idea where I could have gone wrong?</p>
  
  <p>Thanks a lot<br>
  Rü</p>
  
  <p>UserLogon.xaml:</p>
  
  <pre><code>    &lt;ListBox Grid.Column="0" Grid.Row="1" Grid.RowSpan="4" MinWidth="100" Margin="5" Name="lstUser" MouseUp="lstUser_MouseUp"
               ItemsSource="{Binding Path=Users}" SelectedItem="{Binding Path=CurrentUser, Mode=TwoWay}"/&gt;
      &lt;TextBox Grid.Column="1" Grid.Row="1" Margin="3" Name="txtName" TextChanged="txtName_TextChanged"
               Text="{Binding Path=CurrentUser, Mode=OneWay}" /&gt;
  </code></pre>
  
  <p></p>
  
  <p>UserLogon.xaml.cs:</p>
  
  <pre><code>    public UserLogon()
      {
          InitializeComponent();
  
          _logic = new UserLogonLogic();
          TopLevelContainer.DataContext = _logic;
      }
  
      private int _internalChange = 0;
      private void txtName_TextChanged(object sender, TextChangedEventArgs e)
      {
          if (_internalChange &gt; 0)
          {
              return;
          }
  
          _internalChange++;
          string oldName = txtName.Text;
          User user = _logic.SelectByPartialUserName(oldName);
          string newName = (user == null) ? "" : user.Name;
  
          if (oldName != newName)
          {
              txtName.Text = (newName == "") ? oldName : newName;
              txtName.Select(oldName.Length, newName.Length);
          }
          _internalChange--;
      }
  </code></pre>
  
  <p>UserLogon.Logic.cs:</p>
  
  <pre><code>public class UserLogonLogic : INotifyPropertyChanged
  {
      private User _currentUser;
      public User CurrentUser
      {
          get { return _currentUser; }
          set
          {
              if (value != CurrentUser)
              {
                  _currentUser = value;
                  OnPropertyChanged("CurrentUser");
              }
          }
  
      private IEnumerable&lt;User&gt; _users;
      public IEnumerable&lt;User&gt; Users
      {
          get
          {
              if (_users == null)
              {
                  List&lt;User&gt; _users = Database.GetAllUsers();
              }
              return _users;
          }
      }
  
      public event PropertyChangedEventHandler PropertyChanged;
      public void OnPropertyChanged(string prop)
      {
          if (PropertyChanged != null)
          {
              PropertyChanged(this, new PropertyChangedEventArgs(prop));
          }
      }
  
      public User SelectByPartialUserName(string value)
      {
          if (value != "")
          {
              IEnumerable&lt;User&gt; allUser = GetAllUserByName(value);
              if (allUser.Count() &gt; 0)
              {
                  CurrentUser = allUser.First();
              }
              else
              {
                  CurrentUser = null;
              }
          }
          else
          {
              CurrentUser = null;
          }
  
          return CurrentUser;
      }
  
      private IEnumerable&lt;User&gt; GetAllUserByName(string name)
      {
          return from user in Users
                 where user.Name.ToLower().StartsWith(name.ToLower())
                 select user;
      }
  }
  </code></pre>