Implement DeleteElementCommand.

This commit is contained in:
Daniel Kraus
2015-08-05 20:28:09 +02:00
parent 742a588083
commit 60296602a9
4 changed files with 71 additions and 2 deletions

View File

@ -62,10 +62,24 @@ namespace zaaReloaded2.ViewModels
public void AddChildElement(FormatElementViewModel viewModel)
{
Elements.Add(viewModel);
viewModel.Parent = this;
ControlElementBase e = Element as ControlElementBase;
e.FormatElements.Add(viewModel.RevealModelObject() as FormatElementBase);
}
public void RemoveChildElement(FormatElementViewModel viewModel)
{
if (!Elements.Contains(viewModel))
{
throw new InvalidOperationException(
"Cannot remove child view model that is not in the collection.");
}
Elements.Remove(viewModel);
((ControlElementBase)Element).FormatElements
.Remove(viewModel.RevealModelObject() as FormatElementBase);
viewModel.Parent = null;
}
#endregion
}
}

View File

@ -39,6 +39,12 @@ namespace zaaReloaded2.ViewModels
}
}
/// <summary>
/// Gets or sets the parent ControlElementViewModel, if
/// there is any.
/// </summary>
public ControlElementViewModel Parent { get; set; }
#endregion
#region Constructors

View File

@ -299,7 +299,38 @@ namespace zaaReloaded2.ViewModels
return LastSelectedElement is ControlElementViewModel;
}
void DoDeleteElement() { }
/// <summary>
/// Deletes the selected element.
/// </summary>
/// <remarks>
/// The following algorithm ist used to find out whether the selected
/// element is at the first or the second level of the hierarchy:
/// If the Element is a ControlElement, it must be at the first level.
/// If the Element is a FormatElement, its Parent property will be
/// Null if the Element is at the first level.
/// </remarks>
void DoDeleteElement()
{
if (CanDeleteElement())
{
if (LastSelectedElement is ControlElementViewModel ||
((FormatElementViewModel)LastSelectedElement).Parent == null)
{
// First level of the hierarchy
Elements.Remove(LastSelectedElement);
ElementBase element = LastSelectedElement.RevealModelObject() as ElementBase;
_settings.Elements.Remove(element);
}
else
{
// Second level of the hierarchy
FormatElementViewModel formatVM = LastSelectedElement as FormatElementViewModel;
ControlElementViewModel parent = formatVM.Parent;
parent.RemoveChildElement(formatVM);
}
LastSelectedElement = null;
}
}
bool CanDeleteElement() { return LastSelectedElement != null && LastSelectedElement.IsSelected; }