private void RemoveTagButton_Click(object sender, RoutedEventArgs e)
        {
            NoteTag noteTag = null;

            if (!string.IsNullOrWhiteSpace(NewTagNameBox.Text))
            {
                noteTag = currentSelectedNote.NoteTags.FirstOrDefault(nt =>
                                                                      nt.Tag.Name.ToLower() == NewTagNameBox.Text.ToLower());
                currentSelectedNote.NoteTags.Remove(noteTag);
                context.TryUpdateManyToMany(currentSelectedNote.NoteTags, currentSelectedNote.NoteTags, x => x.TagKey);
                context.SaveChanges();
                TagList.ItemsSource = currentSelectedNote.NoteTags.Select(nt => nt.Tag);
                CollectionViewSource.GetDefaultView(TagList.ItemsSource).Refresh();
                NewTagNameBox.Text = "";
                if (!context.NoteTags.Any(nt => nt.Tag == noteTag.Tag))
                {
                    context.Remove(noteTag.Tag);
                    context.SaveChanges();
                }
                NewTagNameBox.Focus();
                return;
            }
            if (TagList.SelectedIndex == -1)
            {
                NewTagNameBox.Focus();
                return;
            }

            noteTag = currentSelectedNote.NoteTags.FirstOrDefault(nt => nt.Tag == (TagList.SelectedItem as Tag));
            currentSelectedNote.NoteTags.Remove(noteTag);
            context.TryUpdateManyToMany(currentSelectedNote.NoteTags, currentSelectedNote.NoteTags, x => x.TagKey);
            context.SaveChanges();
            TagList.ItemsSource = currentSelectedNote.NoteTags.Select(nt => nt.Tag);
            CollectionViewSource.GetDefaultView(TagList.ItemsSource).Refresh();
            NewTagNameBox.Text = "";
            if (!context.NoteTags.Any(nt => nt.Tag == noteTag.Tag))
            {
                context.Remove(noteTag.Tag);
                context.SaveChanges();
            }
            NewTagNameBox.Focus();
        }
        private void AddTagButton_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(NewTagNameBox.Text))
            {
                return;
            }

            if (currentSelectedNote != null)
            {
                if (!(string.IsNullOrWhiteSpace(NoteNameBox.Text) || string.IsNullOrWhiteSpace(NoteContentBox.Text)) &&
                    context.Notes.Any(note => note.Name.ToLower() == currentSelectedNote.Name.ToLower()))
                {
                    SaveNote();
                }
                else
                {
                    MessageBox.Show(this, "Please add a name and some notes to this entry before adding tags.",
                                    "Could not save note", MessageBoxButton.OK);
                    NewTagNameBox.Focus();
                    return;
                }

                if (string.IsNullOrWhiteSpace(NewTagNameBox.Text))
                {
                    MessageBox.Show(this, "Please provide a tag name when adding tags to notes");
                    NewTagNameBox.Focus();
                    return;
                }

                Tag tag = context.Tags.Any(t => t.Name.ToLower() == NewTagNameBox.Text.ToLower()) ?
                          context.Tags.First(t => t.Name.ToLower() == NewTagNameBox.Text.ToLower()) :
                          new Tag()
                {
                    Name = NewTagNameBox.Text
                };

                NoteTag noteTag = new NoteTag()
                {
                    Note = currentSelectedNote, Tag = tag
                };
                try
                {
                    context.NoteTags.Add(noteTag);
                }
                catch
                {
                    MessageBox.Show(this, "This note is already connected to that tag", "Duplicate tag detected",
                                    MessageBoxButton.OK);
                    NewTagNameBox.Focus();
                    return;
                }
                context.SaveChanges();

                TagList.ItemsSource = currentSelectedNote.NoteTags.Select(nt => nt.Tag);
                CollectionViewSource.GetDefaultView(TagList.ItemsSource).Refresh();
                NewTagNameBox.Text = "";
            }
            else
            {
                MessageBox.Show(this, "Please name your note before adding tags", "Could not add tag",
                                MessageBoxButton.OK);
            }
            NewTagNameBox.Focus();
        }