partial void createEmployee(Foundation.NSObject sender) { NSUndoManager undo = this.UndoManager; // Has an edit occurred already in this event? if (undo.GroupingLevel > 0) { // Close the last group undo.EndUndoGrouping(); // Open a new group undo.BeginUndoGrouping(); } Person newEmployee = new Person(); Console.WriteLine("Adding {0} to {1}", newEmployee, Employees); // Undo add NSArray args = NSArray.FromObjects(new object[]{newEmployee}); undo.RegisterUndoWithTarget(this, new Selector("undoAdd:"), args); undo.SetActionname(NSBundle.MainBundle.LocalizedString("ADD_PERSON", null)); Employees.Add(newEmployee); StartObservingPerson(newEmployee); tableView.ReloadData(); int row = Employees.IndexOf(newEmployee); Console.WriteLine("Starting edit of {0} in row {1}", newEmployee, row); // Begin the edit of the first column tableView.SelectRow(row, false); tableView.EditColumn(0, row, null, true); }
public void StopObservingPerson(Person person) { person.RemoveObserver(this, new NSString("name")); person.RemoveObserver(this, new NSString("expectedRaise")); }
partial void btnCreateEmployee(Foundation.NSObject sender) { NSWindow w = tableView.Window; // try to end any editing that is taking place bool editingEnded = w.MakeFirstResponder(w); if (!editingEnded) { Console.WriteLine("Unable to end editing"); return; } NSUndoManager undo = this.UndoManager; // Has an edit occurred already in this event? if (undo.GroupingLevel > 0) { // Close the last group undo.EndUndoGrouping(); // Open a new group undo.BeginUndoGrouping(); } // Create the object // Should be able to do arrayController.NewObject, but it returns an NSObjectController // not an NSObject and also causes an InvalidCastException // BUG: https://bugzilla.xamarin.com/show_bug.cgi?id=25620 // Person p = arrayController.NewObject; // Workaround // Person p = (Person)Runtime.GetNSObject (Messaging.IntPtr_objc_msgSend(arrayController.Handle, Selector.GetHandle ("newObject"))); // Plus I can't figure out how to get the Person object from NSObjectController. Ah, this is due to above bug. // Creating my own Person object instead Person p = new Person(); // Add it to the content array of arrayController arrayController.AddObject(p); // Re-sort (in case the user has sorted a column) arrayController.RearrangeObjects(); // Get the sorted array NSArray a = NSArray.FromNSObjects(arrayController.ArrangedObjects()); // Find the object just added int row = -1; for (nuint i = 0; i < a.Count; i++) { if (p == a.GetItem<Person>(i)) { row = (int)i; break; } } Console.WriteLine("Starting edit of {0} in row {1}", p, row); // Begin the edit of the first column tableView.EditColumn(0, row, null, true); }
public void StartObservingPerson(Person person) { person.AddObserver(this, new NSString("name"), NSKeyValueObservingOptions.Old, this.Handle); person.AddObserver(this, new NSString("expectedRaise"), NSKeyValueObservingOptions.Old, this.Handle); }
public void InsertObjectInEmployeesAtIndex(Person p, int index) { NSUndoManager undo = this.UndoManager; Console.WriteLine("Adding {0} to {1}", p, Employees); // Add the inverse of this operation to the undo stack NSArray args = NSArray.FromObjects(new object[]{p, new NSNumber(index)}); undo.RegisterUndoWithTarget(this, new Selector("undoAdd:"), args); if (!undo.IsUndoing) { undo.SetActionname("Add Person"); } // Add the person to the array this.StartObservingPerson(p); Employees.Insert(p, index); }