|
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using CodeProject.Windows.Markup;
using TaskList.DataModel;
namespace TaskList.ViewModel
{
public class TaskListPageViewModel : TaskListViewModel
{
private CommandBindingCollection commands;
private ITaskListPageView view;
private ObservableCollection<Task> selectedTasks = new ObservableCollection<Task>();
public TaskListPageViewModel(ITaskListPageView view)
{
this.view = view;
}
protected override void SetApplication(ITaskListApplication value)
{
if (TaskListApplication != null)
TaskListApplication.PropertyChanged -= TaskListApplication_PropertyChanged;
base.SetApplication(value);
if (TaskListApplication != null)
TaskListApplication.PropertyChanged += TaskListApplication_PropertyChanged;
}
public TaskCollection Tasks
{
get
{
return TaskListApplication.Tasks;
}
}
public IList<Task> SelectedTasks
{
get
{
return selectedTasks;
}
}
public string FileName
{
get
{
if (string.IsNullOrEmpty(TaskListApplication.FileName))
return "[New]";
return TaskListApplication.FileName;
}
}
[CommandSink]
public CommandBindingCollection Commands
{
get
{
if (commands == null)
{
commands = new CommandBindingCollection();
commands.Add(new CommandBinding(TaskListCommands.AddTask,
OnAddTaskExecuted));
commands.Add(new CommandBinding(TaskListCommands.EditTask,
OnEditTaskExecuted, CanEditTask));
commands.Add(new CommandBinding(TaskListCommands.RemoveTasks,
OnRemoveTasksExecuted, CanRemoveTasks));
}
return commands;
}
}
private void OnAddTaskExecuted(object sender, ExecutedRoutedEventArgs e)
{
Task task = e.Parameter as Task;
if (task == null)
{
task = view.CreateNewTask();
}
if (task != null)
{
Tasks.Add(task);
}
}
private void CanEditTask(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = (SelectedTasks.Count == 1);
}
private void OnEditTaskExecuted(object sender, ExecutedRoutedEventArgs e)
{
view.EditTask(SelectedTasks[0]);
}
private void CanRemoveTasks(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = SelectedTasks.Count > 0;
}
private void OnRemoveTasksExecuted(object sender, ExecutedRoutedEventArgs e)
{
Task[] selected = SelectedTasks.ToArray();
foreach (Task item in selected)
{
Tasks.Remove(item);
}
SelectedTasks.Clear();
}
private void TaskListApplication_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == "Tasks" || e.PropertyName == "FileName")
OnPropertyChanged(e.PropertyName);
}
}
}
|
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.
Windows developer with 10+ years experience working in the banking industry.