private void add_Click(object sender, RoutedEventArgs e) { //<SnippetAddItem> IEditableCollectionView 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. ChangeItemWindow win = new ChangeItemWindow(); //Create a new item to be added to the collection. win.DataContext = editableCollectionView.AddNew(); // 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(); } //</SnippetAddItem> }
private void edit_Click(object sender, RoutedEventArgs e) { if (itemsControl.SelectedItem == null) { MessageBox.Show("No item is selected"); return; } //<SnippetEditItem> IEditableCollectionView editableCollectionView = itemsControl.Items as IEditableCollectionView; // Create a window that prompts the user to edit an item. ChangeItemWindow win = new ChangeItemWindow(); 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 { //<SnippetCancelEdit> // If the objects in the collection can discard pending // changes, calling IEditableCollectionView.CancelEdit // will revert the changes. Otherwise, you must provide // your own logic to revert the changes in the object. if (!editableCollectionView.CanCancelEdit) { // Provide logic to revert changes. } editableCollectionView.CancelEdit(); //</SnippetCancelEdit> } //</SnippetEditItem> }