开发者

How do I update a listview item that is bound to a collection in WPF?

In WPF, I have a ListView bound to an ObservableCollection in the code-behind. I have working code that adds and removes items from the list by updating the collection.

I have an 'Edit' button whi开发者_开发技巧ch opens a dialog and allows the user to edit the values for the selected ListView item. However, when I change the item, the list view is not updated. I'm assuming this is because I'm not actually adding/removing items from the collection but just modifying one of its items.

How do I tell the list view that it needs to synchronize the binding source?


You need to implement INotifyPropertyChanged on the item class, like so:

class ItemClass : INotifyPropertyChanged
{
    public int BoundValue
    {
         get { return m_BoundValue; }
         set
         {
             if (m_BoundValue != value)
             {
                 m_BoundValue = value;
                 OnPropertyChanged("BoundValue")
             }
         }
    }

    void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    int m_BoundValue;
}


Do you have set the binding mode to TwoWay? If not, try to do that.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜