Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 152171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:34:32+00:00 2026-05-11T09:34:32+00:00

I’m new to WPF and its Databinding, but I stumbled upon a strange behaviour

  • 0

I’m new to WPF and its Databinding, but I stumbled upon a strange behaviour I could not resolve for myself.

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 CurrentUser property.

I want the TextBox to update its text when I click on a name in the ListBox. I also want the SelectedItem 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.

At first the TextBox gets updated every time I click into the ListBox. Debug shows me that every time the PropertyChangeEvent for CurrentUser is fired the method txtName_TextChanged method is called. Only after I have typed something into the textbox the DataBinding 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 txtName_TextChanged is no longer being called after the CurrentUser PropertyChangeEvent is fired.

Does anybody have an idea where I could have gone wrong?

Thanks a lot
Rü

UserLogon.xaml:

    <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}'/>     <TextBox Grid.Column='1' Grid.Row='1' Margin='3' Name='txtName' TextChanged='txtName_TextChanged'              Text='{Binding Path=CurrentUser, Mode=OneWay}' /> 

UserLogon.xaml.cs:

    public UserLogon()     {         InitializeComponent();          _logic = new UserLogonLogic();         TopLevelContainer.DataContext = _logic;     }      private int _internalChange = 0;     private void txtName_TextChanged(object sender, TextChangedEventArgs e)     {         if (_internalChange > 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--;     } 

UserLogon.Logic.cs:

public class UserLogonLogic : INotifyPropertyChanged {     private User _currentUser;     public User CurrentUser     {         get { return _currentUser; }         set         {             if (value != CurrentUser)             {                 _currentUser = value;                 OnPropertyChanged('CurrentUser');             }         }      private IEnumerable<User> _users;     public IEnumerable<User> Users     {         get         {             if (_users == null)             {                 List<User> _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<User> allUser = GetAllUserByName(value);             if (allUser.Count() > 0)             {                 CurrentUser = allUser.First();             }             else             {                 CurrentUser = null;             }         }         else         {             CurrentUser = null;         }          return CurrentUser;     }      private IEnumerable<User> GetAllUserByName(string name)     {         return from user in Users                where user.Name.ToLower().StartsWith(name.ToLower())                select user;     } } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T09:34:32+00:00Added an answer on May 11, 2026 at 9:34 am

    This is a job for a good view model. Define two properties on your view model:

    • SelectedUser : User
    • UserEntry : string

    Bind the ListBox‘s SelectedItem to the SelectedUser property, and the TextBox‘s Text property to the UserEntry property. Then, in your view model you can do the work to keep them in sync: – if SelectedUser changes, set UserEntry to that user’s Name – if UserEntry changes, do an intelligent search through all users and set SelectedUser to either null if no match was found, or the first matching User

    Here is a complete and working sample. I wish I could easily attach a zip file right about now.

    First, ViewModel.cs:

    public abstract class ViewModel : INotifyPropertyChanged {     private readonly Dispatcher _dispatcher;      protected ViewModel()     {         if (Application.Current != null)         {             _dispatcher = Application.Current.Dispatcher;         }         else         {             _dispatcher = Dispatcher.CurrentDispatcher;         }     }      public event PropertyChangedEventHandler PropertyChanged;      protected Dispatcher Dispatcher     {         get { return _dispatcher; }     }      protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)     {         var handler = PropertyChanged;          if (handler != null)         {             handler(this, e);         }     }      protected void OnPropertyChanged(string propertyName)     {         OnPropertyChanged(new PropertyChangedEventArgs(propertyName));     } } 

    User.cs:

    public class User : ViewModel {     private readonly string _name;      public User(string name)     {         _name = name;     }      public string Name     {         get { return _name; }     } } 

    LogonViewModel.cs:

    public class LogonViewModel : ViewModel {     private readonly ICollection<User> _users;     private User _selectedUser;     private string _userEntry;      public LogonViewModel()     {         _users = new List<User>();         //fake data         _users.Add(new User('Kent'));         _users.Add(new User('Tempany'));     }      public ICollection<User> Users     {         get { return _users; }     }      public User SelectedUser     {         get { return _selectedUser; }         set         {             if (_selectedUser != value)             {                 _selectedUser = value;                 OnPropertyChanged('SelectedUser');                 UserEntry = value == null ? null : value.Name;             }         }     }      public string UserEntry     {         get { return _userEntry; }         set         {             if (_userEntry != value)             {                 _userEntry = value;                 OnPropertyChanged('UserEntry');                 DoSearch();             }         }     }      private void DoSearch()     {         //do whatever fuzzy logic you want here - I'm just doing a simple match         SelectedUser = Users.FirstOrDefault(user => user.Name.StartsWith(UserEntry, StringComparison.OrdinalIgnoreCase));     } } 

    UserLogon.xaml:

    <UserControl x:Class='WpfApplication1.UserLogon'     xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'     xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'     Height='300' Width='300'>     <StackPanel>         <ListBox ItemsSource='{Binding Users}' SelectedItem='{Binding SelectedUser}' DisplayMemberPath='Name'/>         <TextBox Text='{Binding UserEntry, UpdateSourceTrigger=PropertyChanged}'/>     </StackPanel> </UserControl> 

    UserLogon.xaml.cs:

    public partial class UserLogon : UserControl {     public UserLogon()     {         InitializeComponent();         //would normally map view model to view with a DataTemplate, not manually like this         DataContext = new LogonViewModel();     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm making a simple page using Google Maps API 3. My first. One marker
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.