private void btnAddCutomerToProductClickHandler(object sender, RoutedEventArgs e)
        {
            var f = new CustomersSelectionWindow(this);
            f.ShowDialog();
            var selectedCustomer = f.GetSelectedCustomer();
            if (selectedCustomer == null)
            {
                //this.ShowError("Необходимо выбрать минимум одного покупателя из списка.");
                return;
            }
            var order = new Order
                        {
                            Amount = 0,
                            CustomerId = selectedCustomer.Id,
                            Date = DateTime.Now,
                            InitialVkCommentId = 0,
                            ProductId = m_product.Id,
                            Comment = ""
                        };
            var caw = new ChangeAmountWindow(this, order);
            caw.ShowDialog();

            m_bgworker = new BackgroundWorker();
            m_bgworker.DoWork += (o, args) =>
                {
                    var ord = args.Argument as Order;
                    var ordersRepo = Core.Repositories.DbManger.GetInstance().GetOrderRepository();
                    try
                    {
                        var allProductOrders = ordersRepo.GetOrdersForProduct(m_product);
                        if (allProductOrders.Any(productOrder => productOrder.CustomerId == ord.CustomerId))
                        {
                            throw new BgWorkerException("Указанный покупатель уже заказывал данный товар!");
                        }
                    }
                    catch (BgWorkerException)
                    {
                        throw;
                    }
                    catch (Exception)
                    {
                        throw new BgWorkerException("Не удалось получить данные из БД");
                    }
                    try
                    {
                        ordersRepo.Add(order);
                    }
                    catch (Exception)
                    {
                        throw new BgWorkerException("Не удалось сохранить информацию о заказе в БД");
                    }

                    args.Result = new ProductCustomerListViewItem(order, selectedCustomer);
                };
            m_bgworker.RunWorkerCompleted += (o, args) =>
                {
                    if (args.Error != null)
                    {
                        this.ShowError(args.Error.Message);
                        return;
                    }
                    var item = (ProductCustomerListViewItem) args.Result;
                    m_lvi.OrderedAmount += item.SourceOrderInfo.Amount;
                    lvOrderItems.Items.Add(item);
                };

            m_bgworker.RunWorkerAsync(order);
        }
        private void EventSetter_OnHandler(object sender, MouseButtonEventArgs e)
        {
            if (!(sender is ListViewItem)) return;

            var lvi = ((sender as ListViewItem).Content) as OrderListViewItem;
            if (lvi == null)
                return;

            var caw = new ChangeAmountWindow(this, lvi.SourceOrder);
            caw.ShowDialog();

            if (caw.GetResult() == ChangeAmountWindow.Result.Changed)
            {
                // save changes
                var db = Core.Repositories.DbManger.GetInstance();
                var repo = db.GetOrderRepository();
                try
                {
                    repo.Update(lvi.SourceOrder);
                }
                catch (Exception exception)
                {
                    this.ShowError(String.Format("Ошибка: Не удалось сохранить изменения. ({0})", exception.GetType().Name));
                }
            }
        }
 private void ListViewItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
 {
     if ((e.Source as ListViewItem) == null) return;
     if ((e.Source as ListViewItem).Content is ProductCustomerListViewItem)
     {
         var lviObj = (e.Source as ListViewItem).Content as ProductCustomerListViewItem;
         var oldAmount = lviObj.SourceOrderInfo.Amount;
         var f = new ChangeAmountWindow(this, lviObj.SourceOrderInfo);
         f.ShowDialog();
         if (f.GetResult() == ChangeAmountWindow.Result.Changed)
         {
             // save changes
             var db = Core.Repositories.DbManger.GetInstance();
             var repo = db.GetOrderRepository();
             try
             {
                 repo.Update(lviObj.SourceOrderInfo);
             }
             catch (Exception exception)
             {
                 this.ShowError(String.Format("Ошибка: Не удалось сохранить изменения. ({0})", exception.GetType().Name));
             }
             m_lvi.OrderedAmount += (lviObj.SourceOrderInfo.Amount - oldAmount);
         }
     }
 }