I have an ObservableCollection of objects of a certain class. When I assign it a new value my CollectionChanged is null and UI don't update. If I in constructor set beginning items to Collection like this:
public TabPageViewModel()
{
tabPageDatas = new ObservableCollection<TabPageDataModel>
{
new TabPageDataModel(null, "test1"),
};
}
Then at start my app I see new TabItem, but if I set new value, then no.
public class TabPageViewModel : INotifyCollectionChanged
{
public ObservableCollection<TabPageDataModel> TabPageDatas
{
get => tabPageDatas;
set
{
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add));
}
}
private ObservableCollection<TabPageDataModel> tabPageDatas;
public TabPageViewModel()
{
tabPageDatas = new ObservableCollection<TabPageDataModel>();
}
public event NotifyCollectionChangedEventHandler CollectionChanged;
}
private void AddNewTabItem_Click(object sender, RoutedEventArgs e)
{
ObservableCollection<TabPageDataModel> newTab = tabPageViewModel.TabPageDatas;
newTab.Add(new TabPageDataModel(bitmapImage, "Test2");
tabPageViewModel.TabPageDatas = newTab;
}
XAML:
<TabControl ItemsSource="{Binding Path=TabPageDatas, Mode=TwoWay}">
<TabControl.DataContext>
<ViewModels:TabPageViewModel/>
</TabControl.DataContext>