简体   繁体   中英

Bound Textbox/Labels not updating when OnPropertyChanged fired

I have a simple application which uses a BindingList<(Person)>, people, to store information about employees (Windows Forms). Person has several properties such as Name, DateOfBirth, etc and implements INotifyPropertyChanged.

The BindingList<(Person)>, people, is bound to a binding source. A DataGridView control is bound to this source, and as expected property changes are updated on the DataGridView. For example, when I change the age of a person the DataGridView updates immediately.

My problem occurs when I use the same binding source other controls. I have the text property of a text box bound to Person.Name, using the same binding source as the DataGridView. Changes to the Person.Name property update on the DataGridView, but not to the text box.

How can I make the text box update, like the DataGridView, when a property changes?

Chris

Just quickly, this is untested, it probably needs to be handled by a Dispatcher. Try using the following in place of your TextBox

public class DispatchedTextBox : TextBox
    {
        DispatchEvent _propertyChanged = new DispatchEvent();

        protected override void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            this._propertyChanged.Fire(this, e);
        }

        protected override event PropertyChangedEventHandler PropertyChanged
        {
            add { this._propertyChanged.Add(value); }
            remove { this._propertyChanged.Remove(value); }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM