예제 #1
0
        private void ContextMenu_RightClick(object sender, MouseButtonEventArgs e)
        {
            if (activationFunction)
            {
                Location location = MyMap.ViewportPointToLocation(Mouse.GetPosition(MyMap));
                var      temp     = MyMap.GetCurrentRegion(location, companyEntities);
                if (temp != null && regionVisibility)
                {
                    ContextMenu context = new ContextMenu();
                    context.IsOpen = true;
                    var editRegion = new MenuItem()
                    {
                        Header = "Edytuj region"
                    };
                    var removeRegion = new MenuItem()
                    {
                        Header = "Usuń region"
                    };
                    var infoRegion = new MenuItem()
                    {
                        Header = "Info"
                    };

                    infoRegion.Click += (s, es) => MessageBox.Show($"Kod: {temp.code}");
                    editRegion.Click += async(s, es) =>
                    {
                        MyMap.Children.Remove(MyMap.GetPolyline(temp));
                        while (e.LeftButton == MouseButtonState.Released)
                        {
                            await Task.Delay(25);
                        }
                        var viewModel = (RegionViewModel)this.DataContext;
                        viewModel.WarehouseSelectedRegion = viewModel.Warehouses.FirstOrDefault(w => w.id == temp.idWarehouse);
                        viewModel.EmployeeSelectedRegion  = viewModel.Employees.FirstOrDefault(e => e.idRegion == temp.id);
                        CreateRegions_ClickAsync(s, es);
                        region = temp;
                    };
                    removeRegion.Click += (s, es) =>
                    {
                        System.Windows.Forms.DialogResult result = (System.Windows.Forms.DialogResult)MessageBox.Show($"Na pewno chcesz usunąć region {temp.code}?", "", MessageBoxButton.YesNo, MessageBoxImage.Question);
                        if (result == System.Windows.Forms.DialogResult.Yes)
                        {
                            var emplTemp = companyEntities.Employee.Where(e => e.idRegion == temp.id);
                            foreach (var employee in emplTemp)
                            {
                                employee.idRegion = null;
                            }
                            MyMap.Children.Remove(MyMap.GetPolyline(temp));
                            companyEntities.Localization.Remove(temp.Localization);
                            companyEntities.Localization.Remove(temp.Localization1);
                            temp.idWarehouse = null;
                            companyEntities.Region.Remove(temp);
                            companyEntities.SaveChanges();
                        }
                    };
                    context.Items.Add(editRegion);
                    context.Items.Add(removeRegion);
                    context.Items.Add(infoRegion);
                }
                else
                {
                    ContextMenu context = new ContextMenu();
                    context.IsOpen = true;
                    var createRegions = new MenuItem()
                    {
                        Header = "Dodaj nowy region"
                    };
                    var connectPushPins = new MenuItem()
                    {
                        Header = "Połącz pinezki"
                    };
                    var clear = new MenuItem()
                    {
                        Header = "Wyczyść trase"
                    };
                    clear.Click           += ClearPolyline_Click;
                    connectPushPins.Click += ConnectPushPins_Click;
                    createRegions.Click   += CreateRegions_ClickAsync;
                    context.Items.Add(createRegions);
                    context.Items.Add(connectPushPins);
                    context.Items.Add(clear);
                }
            }
        }