views:

18

answers:

2

Hi folks,

in my Silverlight 4 Application I have an ObservableCollection that I data bind to two different listboxes. The listboxitems showing the content of the MyClass-Object. When I add an item to the ObservableCollection, the new item is displayed in both listboxes properly.

I have set the Binding Mode to two way, so that editing the listboxitems will update the model automatically. This works so far. My problem is, that the content of the other listbox doesn't update with the updated model. Adding a new item will properly show up on the other listbox, but the updates of the content (which I checked happens) won't.

Any ideas how to achieve: The content of the other listbox should update automatically, when the I update the content in one listbox.

Thanks in advance,
Frank

+1  A: 

you need to make sure that your objects in the observable collection implement INotifyPropertyChanged and they should post change events when your content properties change.

luke
this has done the job, thanks!
Aaginor
+2  A: 

To expand on what luke said your class needs to implement INotifyPropertyChanged and your properties need to throw the PropertyChanged event in their setters.

public class MyClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged; // This may be named wrong

    private string _myString = null;

    public string MyString
    {
        get { return _myString; }
        set
        {
            if(value == _myString)
                return;
            _myString = value;
            var eh = PropertyChanged;
            if(eh != null)
                eh(this, new PropertyChangedEventArgs("MyString"));
        }
    }
}

The MyString property will notify the UI that it has changed which will trigger the binding to update.

Stephan
thanks for the code! One error is, that the Event call have to look like: PropertyChanged(this, new PropertyChangedEventArgs("Name"));
Aaginor
I knew that was wrong, but I couldn't remember the exact syntax off the top of my head. Thanks for the correction.
Stephan