ListBox item doesn't get refresh in WPF?

纵然是瞬间 提交于 2019-12-01 08:28:10

Looks like you may need to implement INotifyPropertyChanged in your Task class. This interface alerts the UI that underlying data has changed and it needs to repull the data to update the view.

It's important to note that although the ObservableCollection class broadcasts information about changes to its elements, it doesn't know or care about changes to the properties of its elements. In other words, it doesn't watch for property change notification on the items within its collection. If you need to know if someone has changed a property of one of the items within the collection, you'll need to ensure that the items in the collection implement the INotifyPropertyChanged interface, and you'll need to manually attach property changed event handlers for those objects

Read this article for more info: http://msdn.microsoft.com/en-us/magazine/dd252944.aspx

To implement PropertyChanged notification check this: http://msdn.microsoft.com/en-us/library/ms743695.aspx

Ok here is the working code:

public class Task: INotifyPropertyChanged
    {
        //public string Taskname { get; set; }
        public event PropertyChangedEventHandler PropertyChanged;
        private string _taskname;
        public Task(string value)
        {
            this._taskname = value;
        }
        public string Taskname
        {
            get { return _taskname; }
            set
            {
                _taskname = value;
                OnPropertyChanged("Taskname");
            }
        }
        private void OnPropertyChanged(string value)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(value));
            }
        }
    }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!