|
using System.Collections.ObjectModel;
using Catel.Data;
using Catel.Examples.Models;
using Catel.Examples.PersonApplication.Properties;
using Catel.MVVM;
using Catel.MVVM.Services;
namespace Catel.Examples.PersonApplication.UI.ViewModels
{
/// <summary>
/// Main window view model.
/// </summary>
public class MainWindowViewModel : ViewModelBase
{
#region Variables
#endregion
#region Constructor & destructor
/// <summary>
/// Initializes a new instance of the <see cref="MainWindowViewModel"/> class.
/// </summary>
public MainWindowViewModel()
{
Add = new Command(Add_Execute);
Edit = new Command(Edit_Execute, Edit_CanExecute);
Remove = new Command(Remove_Execute, Remove_CanExecute);
}
#endregion
#region Properties
/// <summary>
/// Gets the title of the view model.
/// </summary>
/// <value>The title.</value>
public override string Title
{
get { return Resources.MainWindowTitle; }
}
/// <summary>
/// Gets the collection of Persons.
/// </summary>
public ObservableCollection<Person> PersonCollection
{
get { return GetValue<ObservableCollection<Person>>(PersonCollectionProperty); }
set { SetValue(PersonCollectionProperty, value); }
}
/// <summary>
/// Register the PersonCollection property so it is known in the class.
/// </summary>
public static readonly PropertyData PersonCollectionProperty = RegisterProperty("PersonCollection", typeof(ObservableCollection<Person>));
/// <summary>
/// Gets or sets the selected person.
/// </summary>
public Person SelectedPerson
{
get { return GetValue<Person>(SelectedPersonProperty); }
set { SetValue(SelectedPersonProperty, value); }
}
/// <summary>
/// Register the SelectedPerson property so it is known in the class.
/// </summary>
public readonly PropertyData SelectedPersonProperty = RegisterProperty("SelectedPerson", typeof(Person), null);
#endregion
#region Commands
/// <summary>
/// Gets the Add command.
/// </summary>
public Command Add { get; private set; }
/// <summary>
/// Method to invoke when the Add command is executed.
/// </summary>
/// <param name="parameter">The parameter of the command.</param>
private void Add_Execute(object parameter)
{
var viewModel = new PersonViewModel(new Person());
var uiVisualizerService = GetService<IUIVisualizerService>();
if (uiVisualizerService.ShowDialog(viewModel) ?? false)
{
PersonCollection.Add(viewModel.Person);
}
}
/// <summary>
/// Gets the Edit command.
/// </summary>
public Command Edit { get; private set; }
/// <summary>
/// Method to check whether the Edit command can be executed.
/// </summary>
/// <param name="parameter">The parameter of the command.</param>
private bool Edit_CanExecute(object parameter)
{
return (SelectedPerson != null);
}
/// <summary>
/// Method to invoke when the Edit command is executed.
/// </summary>
/// <param name="parameter">The parameter of the command.</param>
private void Edit_Execute(object parameter)
{
var viewModel = new PersonViewModel(SelectedPerson);
var uiVisualizerService = GetService<IUIVisualizerService>();
uiVisualizerService.ShowDialog(viewModel);
}
/// <summary>
/// Gets the Remove command.
/// </summary>
public Command Remove { get; private set; }
/// <summary>
/// Method to check whether the Remove command can be executed.
/// </summary>
/// <param name="parameter">The parameter of the command.</param>
private bool Remove_CanExecute(object parameter)
{
return (SelectedPerson != null);
}
/// <summary>
/// Method to invoke when the Remove command is executed.
/// </summary>
/// <param name="parameter">The parameter of the command.</param>
private void Remove_Execute(object parameter)
{
var messageService = GetService<IMessageService>();
if (messageService.Show("Are you sure you want to remove this person?", "Are you sure?", MessageButton.YesNo) == MessageResult.Yes)
{
PersonCollection.Remove(SelectedPerson);
}
}
#endregion
#region Methods
/// <summary>
/// Initializes the object by setting default values.
/// </summary>
protected override void Initialize()
{
PersonCollection = new ObservableCollection<Person>();
PersonCollection.Add(new Person { Gender = Gender.Male, FirstName = "Geert", MiddleName = "van", LastName = "Horrik" });
PersonCollection.Add(new Person { Gender = Gender.Male, FirstName = "Fred", MiddleName = "", LastName = "Retteket" });
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.