private void GetCategories() { int index = -1; if (SelectedCategory != null) { index = CategoriesList.IndexOf(SelectedCategory); } CategoriesList.Clear(); try { CategoriesList = new ObservableCollection <CategoryModel>(storageHandler.GetCategories()); } catch (FileNotFoundException ex) { MessageBox.Show($"File not found {ex.Message}."); } catch (InvalidOperationException ex) { MessageBox.Show($"Can't read data from file {ex.Message}."); } catch (Exception ex) { MessageBox.Show($"Something is wrong {ex.Message}."); } OnPropertyChanged(nameof(CategoriesList)); if (index > -1 && index < CategoriesList.Count) { SelectedCategory = CategoriesList[index]; } else { SelectedCategory = null; } OnPropertyChanged(nameof(SelectedCategory)); }