/// <summary>
 ///     Post selected CategoryViewModel to message hub
 /// </summary>
 protected override void ItemClick(CategoryViewModel category)
 {
     EditCategoryCommand.Execute(category);
 }
示例#2
0
        /// <summary>
        /// Initializes a new instance of the TaskViewModel class.
        /// </summary>
        public TaskViewModel()
        {
            if (IsInDesignMode)
            {
                TestDataService testData = new TestDataService();
                TaskData = testData.Output;
            }
            else
            {
                string FileName = @".\TaskData.xml";
                if (File.Exists(FileName))
                {
                    XmlSerializer reader = new XmlSerializer(typeof(ObservableCollection <Category>));
                    StreamReader  file   = new System.IO.StreamReader(FileName);
                    TaskData = reader.Deserialize(file) as ObservableCollection <Category>;
                }
                else
                {
                    TaskData = new ObservableCollection <Category>();
                    TaskData.Add(new Category()
                    {
                        CategoryName = "Inbox", Index = Guid.NewGuid().ToString(),
                        TaskList     = new ObservableCollection <Task>()
                    });
                    TaskData[0].TaskList.Add(new Task()
                    {
                        TaskName = "Welcome", TaskNote = "", IsFinished = false, SubtaskList = new ObservableCollection <Subtask>()
                    }
                                             );
                    TaskData.Add(new Category()
                    {
                        CategoryName = "Today", Index = Guid.NewGuid().ToString(), TaskList = new ObservableCollection <Task>()
                    });
                    TaskData.Add(new Category()
                    {
                        CategoryName = "Someday", Index = Guid.NewGuid().ToString(), TaskList = new ObservableCollection <Task>()
                    });
                    TaskData.Add(new Category()
                    {
                        CategoryName = "Log", Index = Guid.NewGuid().ToString(), TaskList = new ObservableCollection <Task>()
                    });
                }
            }

            Category_State = "Normal";

            AddTaskCommand          = new RelayCommand(() => AddTask());
            FilterTaskCommand       = new RelayCommand(() => FilterTask());
            ClearFilterCommand      = new RelayCommand(() => { FilterStr = string.Empty; });
            AddTask_EnterKeyCommand = new RelayCommand <KeyEventArgs>((e) =>
            {
                if (e.Key == Key.Enter)
                {
                    AddTask();
                }
            });

            EditCategory_DoubleClick_Command = new RelayCommand <MouseButtonEventArgs>((e) =>
            {
                if (e.ChangedButton == MouseButton.Left && e.ClickCount == 2)
                {
                    EditCategoryCommand.Execute(new object());
                    e.Handled = true;
                }
            });

            EditCategory_Key_Command = new RelayCommand <KeyEventArgs>((e) =>
            {
                if (e.Key == Key.Enter)
                {
                    EditCategoryCommand.Execute(new object());
                }
            });
            EditCategoryCommand = new RelayCommand(() =>
            {
                if (Category_State == "Normal")
                {
                    Messenger.Default.Send <MessengerToken>(MessengerToken.Category_EditMode, "RequestSending_from_TaskVM_to_TaskView_EditCategory");
                    Category_State = "Edit";
                }
                else
                {
                    foreach (Category c in TaskData)
                    {
                        if (string.IsNullOrEmpty(c.CategoryName))
                        {
                            c.CategoryName = "Unnamed";
                        }
                    }
                    Messenger.Default.Send <MessengerToken>(MessengerToken.Category_NormalMode, "RequestSending_from_TaskVM_to_TaskView_EditCategory");
                    Category_State = "Normal";
                }
            });
            DeleteCategoryCommand = new RelayCommand <string>((index) =>
            {
                for (int i = 0; i < TaskData.Count; i++)
                {
                    if (TaskData[i].Index == index)
                    {
                        TaskData.RemoveAt(i);
                        break;
                    }
                }
            });
            AddCategoryCommand = new RelayCommand(() =>
            {
                TaskData.Insert(TaskData.Count - 1, new Category()
                {
                    CategoryName = "Unnamed", Index = Guid.NewGuid().ToString(), TaskList = new ObservableCollection <Task>()
                });
                Messenger.Default.Send <MessengerToken>(MessengerToken.Category_EditMode, "RequestSending_from_TaskVM_to_TaskView_EditCategory");
                Category_State = "Edit";
            });

            Messenger.Default.Register <MessengerToken>(this, "RequestSending_from_MainVM_to_TaskVM_SaveTaskData", false, (msg) =>
            {
                if (msg == MessengerToken.Save)
                {
                    try
                    {
                        XmlSerializer writer = new XmlSerializer(typeof(ObservableCollection <Category>));
                        StreamWriter file    = new StreamWriter(@".\TaskData.xml");
                        writer.Serialize(file, TaskData);
                        file.Close();
                    }
                    catch (Exception e)
                    {
                        Exception ie = e.InnerException;
                    }
                }
            });
            Messenger.Default.Register <MessengerToken>(this, "RequestSending_from_DetailsVM_to_TaskVM_SelectedTask", false, (msg) => { if (msg == MessengerToken.Delete_this_task)
                                                                                                                                        {
                                                                                                                                            RemoveTask();
                                                                                                                                        }
                                                        });
        }