0

I'm making an API which is will be use my chat program.

API core class have a room list like a List<Room> Rooms_;

API core class have a property. public Room[] Rooms { get { return Rooms_.ToArray(); } }

and API core class also have a property which is easily to print room list. This property is public ObservableCollection<Room> ObservableRooms { get { return new ObservableCollection<Room>(Rooms_); }

Now, when room is added or removed, i coded Add function and Remove function like this.

Rooms_.Add(new_room);
Rooms_.Remove(room);

but the ObservableRooms dosen't change automatically.

I want to bind ObservableRooms property to the Rooms_ list.

Anyone know this?

Thanks.

Joy Hyuk Lee
  • 756
  • 11
  • 22
  • I don't think having a property returning an **new** `ObservableCollection` with each call to get makes sense, but I'm not a WPF expert. – Dirk Sep 03 '14 at 06:03
  • @Dirk No, it is a not WPF. just change value automatically :) hmm.. – Joy Hyuk Lee Sep 03 '14 at 06:05
  • @Dirk an ObservableCollection has a constructor (List) but is this wrong? – Joy Hyuk Lee Sep 03 '14 at 06:06
  • 1
    `ObservableCollection` just copies the elements from `List` passed in constructor. It doesn't uses it internally. – Sriram Sakthivel Sep 03 '14 at 06:09
  • The point is that an observable collection is a collection, it stores the values you add and does not rely on any external collection. Initializing it with a list/enumerable simply makes a copy of that list for internal purposes. If you change the original list then the ObservableCollection won't know that. – Dirk Sep 03 '14 at 06:10
  • @SriramSakthivel Ah just copies.. thanks. – Joy Hyuk Lee Sep 03 '14 at 06:15
  • @ Yes.. i will think more about this. – Joy Hyuk Lee Sep 03 '14 at 06:16
  • Are you binding anything to the ObservableCollection? Is it updating something in the UI? – nickm Sep 03 '14 at 06:18
  • Yes. in my client program, there is a listview and i want to bind listview (room infos) with OvseravableRooms. – Joy Hyuk Lee Sep 03 '14 at 06:19
  • Is there a reason why you don't just manipulate the observable collection directly? – nickm Sep 03 '14 at 06:20
  • ObservableCollection will not propagate individual item changes all alone, You have to subscribe to each event. check this - http://stackoverflow.com/questions/1427471/observablecollection-not-noticing-when-item-in-it-changes-even-with-inotifyprop – Abdul Ahad Sep 03 '14 at 06:26

1 Answers1

1

If you are binding directly to the ObservableCollection why not manipulate the ObservableCollection directly.

private ObservableCollection<Room> _observableRooms

public ObservableCollection<Room> ObservableRooms 
{
    get { return _observableRooms; }
    set { _observableRooms = value; }
}

Then add and remove directly from ObservableRooms and your UI should update correctly.

nickm
  • 1,775
  • 1
  • 12
  • 14
  • I would also make the setter private, unless you want users of that API to change the entire collection instead of just adding/deleting items. – Dirk Sep 03 '14 at 07:44