/// <summary>
 /// Remove an item
 /// </summary>
 /// <param name="item">The item</param>
 private void RemoveAction(Item item)
 {
     if (item != null)
     {
         this.Visibility = Visibility.Collapsed;
     }
 }
 /// <summary>
 /// On item removed.
 /// </summary>
 /// <param name="item">The item</param>
 private void OnItemRemoved(Item item)
 {
     item.Category = null;
 }
        /// <summary>
        /// Add an item.
        /// </summary>
        public void AddItem()
        {
            string name = this.Name;
            Category category = this.SelectedCategory;

            if (string.IsNullOrWhiteSpace(name))
            {
                this.Name = string.Empty;
                return;
            }

            // Force the default category
            if (category == null)
            {
                category = this.defaultCategory;
            }

            // 30 characters maximum
            if (name.Length > 30 ||
                category == null)
            {
                return;
            }

            // We keep the selected category
            if (category != null)
            {
                this.defaultCategory = category;
            }

            using (ItemDataContext context = new ItemDataContext(ConnectionString))
            {
                Item itemToCreate = new Item(name, category.Id, false, Visibility.Visible);

                if (context.Items.Where(i => i.Name == itemToCreate.Name &&
                    i.CategoryId == itemToCreate.CategoryId)
                    .FirstOrDefault() == null)
                {
                    context.Items.InsertOnSubmit(itemToCreate);
                    context.SubmitChanges();
                }
            }

            INavigationService navigationService = this.GetService<INavigationService>();
            if (navigationService == null)
            {
                return;
            }

            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("Reload", "true");

            navigationService.Navigate("/Pages/MainPage.xaml", parameters);
        }
 /// <summary>
 /// On item added.
 /// </summary>
 /// <param name="item">The item</param>
 private void OnItemAdded(Item item)
 {
     item.Category = this;
 }