开发者

Bound Textbox/Labels not updating when OnPropertyChanged fired

开发者 https://www.devze.com 2023-01-09 06:34 出处:网络
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 implem

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 开发者_Go百科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); }
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消