public MainWindow()
        {
            // Read the List from database
            using (ToDoEntities da = new ToDoEntities())
            {
                IEnumerable<ToDoItem> ToDoItems = from t in da.ToDoItems
                                                  select t;
                ToDoList = new ObservableCollection<ToDoItem>(ToDoItems);
            }

            InitializeComponent();

            // Sort the displayed list
            collectionView = CollectionViewSource.GetDefaultView(ToDoList) as ListCollectionView;
            collectionView.SortDescriptions.Add(new SortDescription("Index", ListSortDirection.Descending));

            // Search filter
            collectionView.Filter = delegate(object obj)
            {
                ToDoItem item = obj as ToDoItem;
                return IsContainingText(item, SearchTextBox.Text);
            };

            ToDoListBox.ItemsSource = collectionView;

            // Reset all indices
            int i = ToDoList.Count;
            foreach (ToDoItem item in collectionView)
            {
                using (ToDoEntities da = new ToDoEntities())
                {
                    ToDoItem currentItem = da.ToDoItems.FirstOrDefault<ToDoItem>(a => a.Id == item.Id);
                    if (currentItem != null)
                        currentItem.Index = --i;
                    da.SaveChanges();
                    item.Index = i;
                }
            }
        }
        private void addButton_Click(object sender, RoutedEventArgs e)
        {
            ToDoItem item = new ToDoItem();
            AddDialog addDialog = new AddDialog(ref item);
            Nullable<bool> dialogResult = addDialog.ShowDialog();
            if (dialogResult == true)
            {
                //Get new Index
                if (ToDoList.Count == 0)
                    item.Index = 0;
                else
                {
                    var maxIndex = ToDoList.Max(m => m.Index);
                    item.Index = ++maxIndex;
                }

                //Get new GUID
                item.Id = Guid.NewGuid();

                //Save the item to db
                int rowsAffected = 0;
                using (ToDoEntities da = new ToDoEntities())
                {
                    da.ToDoItems.AddObject(item);
                    rowsAffected = da.SaveChanges();
                }

                // Save the item to the list
                if (rowsAffected != 0)
                    ToDoList.Add(item);
                else
                    MessageBox.Show("Error while accessing the database", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
 private void ToDoListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
     // Record Changed 'Done' status to database
     if (e.AddedItems.Count != 0 || e.RemovedItems.Count != 0)
     {
         using (ToDoEntities da = new ToDoEntities())
         {
             foreach (object addedObject in e.AddedItems)
             {
                 ToDoItem addedItem = da.ToDoItems.FirstOrDefault<ToDoItem>(p => p.Id == ((ToDoItem)addedObject).Id);
                 if (addedItem != null)
                     addedItem.Done = true;
             }
             foreach (object removedObject in e.RemovedItems)
             {
                 ToDoItem removedItem = da.ToDoItems.FirstOrDefault<ToDoItem>(p => p.Id == ((ToDoItem)removedObject).Id);
                 if (removedItem != null)
                     removedItem.Done = false;
             }
             da.SaveChanges();
         }
     }
 }
        private void ToDoListBox_PreviewDragEnter(object sender, DragEventArgs e)
        {
            ListBox parent = sender as ListBox;
            ToDoItem item = e.Data.GetData(typeof(ToDoItem)) as ToDoItem;
            ToDoItem target = GetItemFromPoint(parent, e.GetPosition(parent)) as ToDoItem;

            if (item == target)
                canMoveItems = true;

            if (item != target && item != null && target != null && canMoveItems)
            {
                int itemIndex = int.Parse(item.Index.ToString());
                int targetIndex = int.Parse(target.Index.ToString());

                if (itemIndex < targetIndex)
                    for (int i = itemIndex + 1; i <= targetIndex; i++)
                    {
                        ToDoItem currentItem = ToDoList.FirstOrDefault<ToDoItem>(a => a.Index == i);
                        if (currentItem != null)
                            currentItem.Index--;

                        using (ToDoEntities da = new ToDoEntities())
                        {
                            currentItem = da.ToDoItems.FirstOrDefault<ToDoItem>(a => a.Index == i);
                            if (currentItem != null)
                                currentItem.Index--;
                            da.SaveChanges();
                        }
                    }
                else if (itemIndex > targetIndex)
                    for (int i = itemIndex - 1; i >= targetIndex; i--)
                    {
                        ToDoItem currentItem = ToDoList.FirstOrDefault<ToDoItem>(a => a.Index == i);
                        if (currentItem != null)
                            currentItem.Index++;

                        using (ToDoEntities da = new ToDoEntities())
                        {
                            currentItem = da.ToDoItems.FirstOrDefault<ToDoItem>(a => a.Index == i);
                            if (currentItem != null)
                                currentItem.Index++;
                            da.SaveChanges();
                        }
                    }

                ToDoList[ToDoList.IndexOf(item)].Index = targetIndex;
                using (ToDoEntities da = new ToDoEntities())
                {
                    ToDoItem currentItem = da.ToDoItems.FirstOrDefault<ToDoItem>(a => a.Id == item.Id);
                    if (currentItem != null)
                        currentItem.Index = targetIndex;
                    da.SaveChanges();
                }

                collectionView.Refresh();
                canMoveItems = false;
            }
        }
        private void editButton_Click(object sender, RoutedEventArgs e)
        {
            // Retrieve the item being edited
            ToDoItem item = ((Button)sender).DataContext as ToDoItem;

            // Call the Add/Edit window
            AddDialog addDialog = new AddDialog(ref item);
            Nullable<bool> dialogResult = addDialog.ShowDialog();

            // Save the edited item
            if (dialogResult == true)
            {
                //Save the changes to the database
                int rowsAffected = 0;
                using (ToDoEntities da = new ToDoEntities())
                {
                    ToDoItem daItem = da.ToDoItems.FirstOrDefault<ToDoItem>(p => p.Id == item.Id);
                    daItem.Index = item.Index;
                    daItem.Title = item.Title;
                    daItem.Description = item.Description;
                    daItem.DueDateTime = item.DueDateTime;
                    daItem.Done = item.Done;

                    rowsAffected = da.SaveChanges();
                }

                if (rowsAffected != 0)
                {
                    int index = ToDoList.IndexOf(item);
                    ToDoList[index] = item;
                }
                else
                    MessageBox.Show("Error while accessing the database", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        private void deleteButton_Click(object sender, RoutedEventArgs e)
        {
            // Retrieve the deleted item
            ToDoItem item = ((Button)sender).DataContext as ToDoItem;

            // Delete the item from the database
            int rowsAffected = 0;
            using (ToDoEntities da = new ToDoEntities())
            {
                IQueryable<ToDoItem> toDelete = from t in da.ToDoItems
                                                where t.Id == item.Id
                                                select t;

                da.ToDoItems.DeleteObject(toDelete.Single());
                rowsAffected = da.SaveChanges();
            }

            // Delete the item from the list
            if (rowsAffected != 0)
                ToDoList.Remove(item);
            else
                MessageBox.Show("Error while accessing the database", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        }