private void Add_Click(object sender, RoutedEventArgs e) { var editableCollectionView = ItemsControl.Items as IEditableCollectionView; if (!editableCollectionView.CanAddNew) { MessageBox.Show("You cannot add items to the list."); return; } // Create a window that prompts the user to enter a new // item to sell. var win = new ChangeItem { DataContext = editableCollectionView.AddNew() }; //Create a new item to be added to the collection. // If the user submits the new item, commit the new // object to the collection. If the user cancels // adding the new item, discard the new item. if ((bool)win.ShowDialog()) { editableCollectionView.CommitNew(); } else { editableCollectionView.CancelNew(); } }
private void Edit_Click(object sender, RoutedEventArgs e) { if (ItemsControl.SelectedItem == null) { MessageBox.Show("No item is selected"); return; } var editableCollectionView = ItemsControl.Items as IEditableCollectionView; // Create a window that prompts the user to edit an item. var win = new ChangeItem(); editableCollectionView.EditItem(ItemsControl.SelectedItem); win.DataContext = ItemsControl.SelectedItem; // If the user submits the new item, commit the changes. // If the user cancels the edits, discard the changes. if ((bool)win.ShowDialog()) { editableCollectionView.CommitEdit(); } else { editableCollectionView.CancelEdit(); } }