0

So I have three things:

  • A ListBox in a Window
  • A DataBase class granting me access to an ObservableList
  • Contact implements INotifyChanged

In my main Window, I have three Buttons (One for new List Entry, one for editing, one for deleting an item)

I fill the list like this:

lbKontakte.ItemsSource = DB.GetInstance().Kontakte;

whereas Kontakte is a ObservableCollection

I can create a new Entry using

DB.GetInstance().Kontakte.Add(New Kontakt(...));

or remove an entry using

DB.GetInstance().Kontakte.Remove(...);

Boh actions are immediately visible in the ListBox.

If I modify a value however, I'm not using any Code. I have a TextBox which is bound to the Name field of a contact. If I make changes to it, the changes should theoretically be carried out immediately to the bound Contact Object.

However, if I do modify the text, the changes do not become visible in the ListBox. If I pause the code and take a look at the object, I can see its Name Field has correctly been changed.

How come my ListBox is not updated?

PS: Contact does implement INotifyChanged using following Code:

public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, e);
        }
    }

and

public String Name
    {
        get { return _name; }
        set { _name = value; OnPropertyChanged(new PropertyChangedEventArgs("Name")); }
    }

Edit: The Textbox is NOT part of the main Window but a Window showed as a dialog if the user clicks the edit button. The Window is then given the selectedItem casted as Kontakt in the Constructor. Bound to the Field like this:

<TextBox Name="txtName" Grid.Column="1" Grid.Row="0" Margin="4" Text="{Binding Path=Name}"></TextBox>

and

public KontaktAddUI(Kontakt kontaktToEdit)
    {
        InitializeComponent();
        this.kontaktToEdit = kontaktToEdit;
        this.MainGrid.DataContext = kontaktToEdit;
    }

Correct Answer in the Comments, thanks again!

Eisenhorn
  • 1,702
  • 1
  • 13
  • 20

1 Answers1

1

Your problem is ObservableCollection doesn't get notified if your Item Property Changed that is a known issue
To fix this problem you need to wire up your INotifyPropertyChanged event to the CollectionChanged event from your ObservableCollection

Here you can see an example how you could do it.

Community
  • 1
  • 1
WiiMaxx
  • 5,322
  • 8
  • 51
  • 89