/// <summary> /// Removes the property from an associated collection/parent. /// </summary> public override void Remove() { var rootModel = this.Parent as IRootModel; if (rootModel != null) { var e = new PropertyModelChildChangeEventArgs(rootModel, this); this.RaiseChildPropertyRemovingEvent(e); if (!e.Cancel) { if (!dictionary.Remove(key)) { return; } rootModel.Refresh(PropertyRefreshReason.CollectionItemRemoved); this.RaiseChildPropertyRemovedEvent(new PropertyModelChildChangeEventArgs(rootModel, this)); // Focus the first entry var propGrid = rootModel.Source as PropertyGrid; if (propGrid != null) { var propertyModel = propGrid.Items.OfType <IPropertyModel>().FirstOrDefault(); if (propertyModel != null) { propGrid.FocusItem(propertyModel); } } } } }
/// <summary> /// Occurs after a <see cref="IPropertyModel"/> representing a child is added to a parent <see cref="IPropertyModel"/>. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <c>PropertyModelChildChangeEventArgs</c> that contains the event data.</param> private void OnPropertyGridChildPropertyAdded(object sender, PropertyModelChildChangeEventArgs e) { var person = e.ParentPropertyModel.Target as Person; var message = string.Format("Added child to {0}, {1}", person.LastName, person.FirstName); this.AppendMessage(message); e.Handled = true; }
/// <summary> /// Occurs after a <see cref="IPropertyModel"/> representing a child is removed from a parent <see cref="IPropertyModel"/>. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <c>PropertyModelChildChangeEventArgs</c> that contains the event data.</param> private void OnPropertyGridChildPropertyRemoved(object sender, PropertyModelChildChangeEventArgs e) { var parent = e.ParentPropertyModel.Target as Person; var child = e.ChildPropertyModel.Value as Person; var message = string.Format("Removed child {0}, {1} from parent {2}, {3}", child.LastName, child.FirstName, parent.LastName, parent.FirstName); this.AppendMessage(message); e.Handled = true; }
/// <summary> /// Occurs before a <see cref="IPropertyModel"/> representing a child is added to a parent <see cref="IPropertyModel"/>. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <c>PropertyModelChildChangeEventArgs</c> that contains the event data.</param> private void OnPropertyGridChildPropertyAdding(object sender, PropertyModelChildChangeEventArgs e) { var person = e.ParentPropertyModel.Target as Person; var message = string.Format("Adding child to {0}, {1}", person.LastName, person.FirstName); this.AppendMessage(message); var result = MessageBox.Show("Are you sure you want to add a new child?", "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question); e.Cancel = (MessageBoxResult.No == result); e.Handled = true; }
/// <summary> /// Occurs before a <see cref="IPropertyModel"/> representing a child is removed from a parent <see cref="IPropertyModel"/>. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <c>PropertyModelChildChangeEventArgs</c> that contains the event data.</param> private void OnPropertyGridChildPropertyRemoving(object sender, PropertyModelChildChangeEventArgs e) { var parent = e.ParentPropertyModel.Target as Person; var child = e.ChildPropertyModel.Value as Person; var message = string.Format("Removing child {0}, {1} from parent {2}, {3}", child.LastName, child.FirstName, parent.LastName, parent.FirstName); this.AppendMessage(message); var result = MessageBox.Show(string.Format("Are you sure you want to remove {0}, {1}?", child.LastName, child.FirstName), "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question); e.Cancel = (MessageBoxResult.No == result); e.Handled = true; }
///////////////////////////////////////////////////////////////////////////////////////////////////// // PUBLIC PROCEDURES ///////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Adds a new child into to an associated collection/parent. /// </summary> public override void AddChild() { if (this.CanAddChild) { var rootModel = ((IDataModel)this).Parent as IRootModel; if (rootModel != null) { var dictionary = rootModel.Value as Dictionary <string, string>; if (dictionary != null) { var entry = new KeyValuePair <string, string>(this.DisplayName, this.ValueAsString); var e = new PropertyModelChildChangeEventArgs(rootModel, entry); this.RaiseChildPropertyAddingEvent(e); if (!e.Cancel) { dictionary[entry.Key] = entry.Value; rootModel.Refresh(PropertyRefreshReason.CollectionItemAdded); this.RaiseChildPropertyAddedEvent(new PropertyModelChildChangeEventArgs(rootModel, entry)); this.DisplayName = null; this.ValueAsString = null; // Focus the entry that was created var propGrid = rootModel.Source as PropertyGrid; if (propGrid != null) { var propertyModel = propGrid.Items.OfType <IPropertyModel>().FirstOrDefault(m => m.Name == entry.Key); if (propertyModel != null) { propGrid.FocusItem(propertyModel); } } } } } } }