Implement DeleteElementCommand.
This commit is contained in:
parent
742a588083
commit
60296602a9
@ -106,7 +106,25 @@ namespace Tests.ViewModels
|
||||
[Test]
|
||||
public void DeleteElement()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
Settings settings = new Settings();
|
||||
settings.AddElement(new SelectFirstDay());
|
||||
settings.AddElement(new SelectEachDay());
|
||||
settings.AddElement(new SelectLastDay());
|
||||
SettingsViewModel settingsVM = new SettingsViewModel(settings);
|
||||
int oldCount = settingsVM.Elements.Count;
|
||||
ElementViewModel elementVM = settingsVM.Elements.First();
|
||||
ElementBase element = elementVM.RevealModelObject() as ElementBase;
|
||||
Assert.IsTrue(settings.Elements.Contains(element),
|
||||
"Settings object does not contain the model that the VM revealed?!");
|
||||
elementVM.IsSelected = true;
|
||||
Assert.IsTrue(settingsVM.DeleteElementCommand.CanExecute(null),
|
||||
"DeleteElementCommand should be enabled.");
|
||||
settingsVM.DeleteElementCommand.Execute(null);
|
||||
Assert.AreEqual(oldCount - 1, settingsVM.Elements.Count);
|
||||
Assert.IsFalse(settingsVM.Elements.Contains(elementVM),
|
||||
"Elements collection still contains the deleted element view model.");
|
||||
Assert.IsFalse(settings.Elements.Contains(element),
|
||||
"Settings' Element collection still contains the element model.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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; }
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user