Fix LastSelected and LastSelectedElement properties.

This commit is contained in:
Daniel Kraus 2015-08-04 06:21:48 +02:00
parent 5fe7250b53
commit 8d6ed32a84
2 changed files with 48 additions and 22 deletions

View File

@ -33,7 +33,21 @@ namespace zaaReloaded2.ViewModels
public ObservableCollection<SettingsViewModel> SettingsList { get; private set; }
public SettingsViewModel Selected { get; private set; }
/// <summary>
/// Gets the SettingsViewModel that was most recently selected. Whether
/// this view model is still selected can be found out be getting the
/// view model's IsSelected property.
/// </summary>
/// <remarks>
/// Due to the way the WPF ListBox (for example) is implemented, selecting
/// a list item will trigger an PropertyChanged event twice: Once for the
/// item being selected, and once for the item being deselected. Thus we
/// can only capture the last item that actually was selected. We cannot
/// howeve capture if an item was deselected without a new selection,
/// because we cannot logicaly connect two occurrences of the same event
/// from different objects.
/// </remarks>
public SettingsViewModel LastSelected { get; private set; }
#endregion
@ -195,23 +209,23 @@ namespace zaaReloaded2.ViewModels
{
if (CanEditSettings())
{
EditSettingsMessage.Send(new ViewModelMessageContent(Selected));
EditSettingsMessage.Send(new ViewModelMessageContent(LastSelected));
}
}
bool CanEditSettings()
{
return Selected != null && !IsDefaultSettings();
return LastSelected != null && LastSelected.IsSelected && !IsDefaultSettings();
}
void DoUseSettings()
{
UseSettingsMessage.Send(new ViewModelMessageContent(Selected));
UseSettingsMessage.Send(new ViewModelMessageContent(LastSelected));
}
bool CanUseSettings()
{
return Selected != null;
return LastSelected != null && LastSelected.IsSelected;
}
void DoAddSettings()
@ -225,7 +239,7 @@ namespace zaaReloaded2.ViewModels
bool CanDeleteSettings()
{
return Selected != null && !IsDefaultSettings();
return LastSelected != null && LastSelected.IsSelected && !IsDefaultSettings();
}
void DoDeleteSettings()
@ -233,7 +247,7 @@ namespace zaaReloaded2.ViewModels
if (CanDeleteSettings())
{
ConfirmDeleteSettingsMessage.Send(
new ViewModelMessageContent(Selected),
new ViewModelMessageContent(LastSelected),
param => ConfirmDeleteSettings(param));
}
}
@ -265,12 +279,12 @@ namespace zaaReloaded2.ViewModels
void DoCopySettings()
{
if (Selected != null)
if (LastSelected != null)
{
SettingsViewModel copy = Selected.Clone() as SettingsViewModel;
SettingsViewModel copy = LastSelected.Clone() as SettingsViewModel;
_repository.SettingsList.Add(copy.RevealModelObject() as Settings);
AddSettingsViewModel(copy);
Selected.IsSelected = false;
LastSelected.IsSelected = false;
copy.IsSelected = true;
}
}
@ -280,7 +294,7 @@ namespace zaaReloaded2.ViewModels
SettingsViewModel vm = sender as SettingsViewModel;
if (vm != null && e.PropertyName == "IsSelected")
{
Selected = vm.IsSelected ? vm : null;
if (vm.IsSelected) LastSelected = vm;
}
}
@ -290,11 +304,11 @@ namespace zaaReloaded2.ViewModels
/// </summary>
bool IsDefaultSettings()
{
if (Selected != null)
if (LastSelected != null)
{
return
(Selected.Name == zaaReloaded2.Properties.Settings.Default.SettingsNameClinic
|| Selected.Name == zaaReloaded2.Properties.Settings.Default.SettingsNameWard);
(LastSelected.Name == zaaReloaded2.Properties.Settings.Default.SettingsNameClinic
|| LastSelected.Name == zaaReloaded2.Properties.Settings.Default.SettingsNameWard);
}
else
{

View File

@ -76,7 +76,16 @@ namespace zaaReloaded2.ViewModels
/// <summary>
/// Gets or sets the currently selected element.
/// </summary>
public ElementViewModel SelectedElement
/// <remarks>
/// Due to the way the WPF ListBox (for example) is implemented, selecting
/// a list item will trigger an PropertyChanged event twice: Once for the
/// item being selected, and once for the item being deselected. Thus we
/// can only capture the last item that actually was selected. We cannot
/// howeve capture if an item was deselected without a new selection,
/// because we cannot logicaly connect two occurrences of the same event
/// from different objects.
/// </remarks>
public ElementViewModel LastSelectedElement
{
get { return _selectedElement; }
set
@ -279,7 +288,7 @@ namespace zaaReloaded2.ViewModels
picker.ElementChosenMessage.Sent += (sender, args) =>
{
FormatElementViewModel newVM = args.Content.ViewModel as FormatElementViewModel;
AddChildElementViewModel(SelectedElement as ControlElementViewModel, newVM);
AddChildElementViewModel(LastSelectedElement as ControlElementViewModel, newVM);
};
AddChildElementMessage.Send(new ViewModelMessageContent(picker));
}
@ -287,27 +296,30 @@ namespace zaaReloaded2.ViewModels
bool CanAddChildElement()
{
return SelectedElement is ControlElementViewModel;
return LastSelectedElement is ControlElementViewModel;
}
void DoDeleteElement() { }
bool CanDeleteElement() { return _selectedElement != null; }
bool CanDeleteElement() { return LastSelectedElement != null && LastSelectedElement.IsSelected; }
void DoCopyElement() { }
bool CanCopyElement() { return _selectedElement != null; }
bool CanCopyElement() { return LastSelectedElement != null && LastSelectedElement.IsSelected; }
/// <summary>
/// Sets or unsets the SelectedElement property whenever the IsSelected
/// property of an ElementViewModel changes.
/// Sets LastSelectedElement property whenever the IsSelected
/// property of an ElementViewModel changes
/// </summary>
/// <remarks>
/// Please see the remarks on the LastSelectedElement property.
/// </remarks>
void ElementViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
ElementViewModel vm = sender as ElementViewModel;
if (vm != null && e.PropertyName == "IsSelected")
{
SelectedElement = vm.IsSelected ? vm : null;
if (vm.IsSelected) LastSelectedElement = vm;
}
}