Reputation: 323
I set DataContext
:
this.DataContext = new MainWindowViewModel();
And I am binding the ItemsSource
of a TabControl
, when I add a new TabItem
in the contructor of MainWindowViewModel
it is working! But when I add a new TabItem
in an event (Click) there is no effect.
I have this property:
List<Item> _listOfItem;
public List<Item> ListOfItem
{
get
{
return _listOfItem;
}
set
{
_listOfItem = value;
PropertyChanged(this, new PropertyChangedEventArgs("ListOfItem"));
}
}
Please help.
Upvotes: 2
Views: 4293
Reputation: 1435
List will not work.
You should use ObservableCollection for the ListOfItem.
Upvotes: 1
Reputation: 45096
You need to use and ObservableCollection for the UI to see additions and deletions to the collection. It worked in the constructor as the List is was built for the the UI.
Upvotes: 1
Reputation: 34349
You should use an ObservableCollection
, rather than a List
if you wish the UI to be notified of collection changes.
ObservableCollection<Item> _listOfItem;
public ObservableCollection<Item> ListOfItem
{
get
{
return _listOfItem;
}
set
{
_listOfItem = value;
PropertyChanged(this, new PropertyChangedEventArgs("ListOfItem"));
}
}
Note that you only need to invoke the PropertyChanged
event for your ListOfItem
if the reference changes after construction of your view model type. If it doesn't change, then a simple auto property will suffice for ListOfItem
.
Upvotes: 2
Reputation: 184386
For collection changes you need the source collection to implement INotifyCollectionChanged
, you could use an ObservableCollection<T>
(which implements it) instead of a List<T>
.
Upvotes: 1