partial void DeleteItemCategory(ItemCategory instance);
 partial void InsertItemCategory(ItemCategory instance);
 partial void UpdateItemCategory(ItemCategory instance);
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string uid = (sender as Button).Uid;
            SuccessMessgage.Visibility = System.Windows.Visibility.Collapsed;
            saveButton.Visibility = System.Windows.Visibility.Visible;

            if (dispathTimer != null) {
                dispathTimer.Stop();
                dispathTimer = null;
            }

            if (uid == "addNew") {
                dgItems.SelectedItem = null;
                toEditItem = null;
            }

            if (uid == "rowEdit") {
                pawnguardDb = new PawnGuardDBDataContext();
                toEditItem = pawnguardDb.ItemCategories
                             .SingleOrDefault<ItemCategory>(pos => pos.id.ToString() == (sender as Button).Content.ToString());

                dgItems.SelectedItem = toEditItem;
                VisualStateManager.GoToElementState(this.LayoutRoot, "ShowState", true);
            }

            if (uid == "closePanel") {
                VisualStateManager.GoToElementState(this.LayoutRoot, "HideState", true);
                FillGrid();
                page_Loaded(sender, e);
            }

            if (uid == "deleteAll") {
                if (toDeleteItem.Count > 0) {
                    pawnguardDb = new PawnGuardDBDataContext();
                    List<ItemCategory> deleteUser = new List<ItemCategory>();
                    for (int i = 0; i < toDeleteItem.Count; i++) {
                        var itemCategory = pawnguardDb.ItemCategories
                                   .FirstOrDefault(usr => usr.id == toDeleteItem[i].id);

                        deleteUser.Add(itemCategory);
                    }

                    if (deleteUser.Count > 0) {
                        pawnguardDb.ItemCategories.DeleteAllOnSubmit(deleteUser);
                        pawnguardDb.SubmitChanges();
                        isCheckAll.IsChecked = false;
                        toDeleteItem.Clear();

                        FillGrid();
                        page_Loaded(sender, e);
                    }
                }
            }

            if (uid == "save") {
                if (toEditItem == null) {

                    if (!IsValid())
                        return;

                    pawnguardDb = new PawnGuardDBDataContext();
                    bool exist = pawnguardDb.ItemCategories.AsEnumerable()
                                 .Where(itm => itm.main.ToLower() == txtCategory.Text.ToLower() ||
                                               itm.option1.ToLower() == txtOption1.Text.ToLower() ||
                                               itm.option2.ToLower() == txtOption2.Text.ToLower())
                                 .Any();

                    if (exist) {
                        categoryError.Visibility = System.Windows.Visibility.Visible;
                        errorMsgCategory.Text = MSG_EXIST;
                        return;
                    }

                    decimal price = 0;
                    decimal.TryParse(txtPrice.Text, out price);
                    pawnguardDb.ItemCategories.InsertOnSubmit(new ItemCategory() {
                        parentId = AppData.parentId,
                        code = txtCode.Text,
                        main = txtCategory.Text,
                        option1 = txtOption1.Text,
                        option2 = txtOption2.Text,
                        price = price,
                        createdAt = DateTime.Now,
                        updatedAt = DateTime.Now
                    });
                } else {
                    if (!IsValid())
                        return;

                    string editEmail = toEditItem.main;
                    string editoption1 = toEditItem.option1;
                    string editoption2 = toEditItem.option2;
                    if (!editEmail.Equals(txtCategory.Text, StringComparison.InvariantCultureIgnoreCase) &&
                        !editoption1.Equals(txtOption1.Text, StringComparison.InvariantCultureIgnoreCase) &&
                        !editoption2.Equals(txtOption2.Text, StringComparison.InvariantCultureIgnoreCase)) {

                        bool exist = pawnguardDb.ItemCategories.AsEnumerable()
                                .Where(itm => itm.main.ToLower() == txtCategory.Text.ToLower() ||
                                                itm.option1.ToLower() == txtOption1.Text.ToLower() ||
                                                itm.option2.ToLower() == txtOption2.Text.ToLower())
                                .Any();

                        if (exist) {
                            categoryError.Visibility = System.Windows.Visibility.Visible;
                            errorMsgCategory.Text = MSG_EXIST;
                            return;
                        }
                    }

                    decimal price = 0;
                    decimal.TryParse(txtPrice.Text, out price);
                    toEditItem.parentId = AppData.parentId;
                    toEditItem.code = txtCode.Text;
                    toEditItem.main = txtCategory.Text;
                    toEditItem.option1 = txtOption1.Text;
                    toEditItem.option2 = txtOption2.Text;
                    toEditItem.price = price;
                    toEditItem.updatedAt = DateTime.Now;
                }

                try {
                    pawnguardDb.SubmitChanges();
                    saveButton.Visibility = System.Windows.Visibility.Collapsed;

                    dispathTimer = new DispatcherTimer();
                    dispathTimer.Interval = new TimeSpan(0, 0, 1);
                    dispathTimer.Start();

                    int time = 3;
                    SuccessMessgage.Visibility = System.Windows.Visibility.Visible;
                    dispathTimer.Tick += delegate(object s, EventArgs ea) {
                        if (time >= 0)
                            runTime.Text = time + " sec.";
                        else {
                            dispathTimer.Stop();
                            SuccessMessgage.Visibility = System.Windows.Visibility.Collapsed;
                            VisualStateManager.GoToElementState(this.LayoutRoot, "HideState", true);
                            runTime.Text = "3 sec.";

                            FillGrid();
                            page_Loaded(sender, e);
                        }
                        time--;
                    };
                } catch (Exception) {
                }
            }
        }