private void OnRegionChanged(object sender, EventArgs e)
        {
            RegionEditorControl regionControl = (RegionEditorControl)sender;

            RegionEditorViewModel regionEditorViewModel = (RegionEditorViewModel)regionControl.DataContext;

            ImageRegion regionObject = regionEditorViewModel.Region;

            // Update size in case is changed
            regionObject.Left   = EnsureValidNormalizedValue(regionControl.Margin.Left / imageControl.ActualWidth);
            regionObject.Top    = EnsureValidNormalizedValue(regionControl.Margin.Top / imageControl.ActualHeight);
            regionObject.Width  = EnsureValidNormalizedValue(regionControl.ActualWidth / imageControl.ActualWidth);
            regionObject.Height = EnsureValidNormalizedValue(regionControl.ActualHeight / imageControl.ActualHeight);

            if (regionObject.Width + regionObject.Left > 1)
            {
                regionObject.Width = 1 - regionObject.Left;
            }

            if (regionObject.Height + regionObject.Top > 1)
            {
                regionObject.Height = 1 - regionObject.Top;
            }

            needsSaving = true;
        }
        private void AddRegionToUI(ImageRegion region)
        {
            var editor = new RegionEditorControl
            {
                Width       = imageControl.ActualWidth * region.Width,
                Height      = imageControl.ActualHeight * region.Height,
                Margin      = new Thickness(region.Left * imageControl.ActualWidth, region.Top * imageControl.ActualHeight, 0, 0),
                DataContext = new RegionEditorViewModel
                {
                    Region               = region,
                    AvailableTags        = this.imageViewModel.AvailableTags,
                    TagHintForNewRegions = this.imageViewModel.TagHintForNewRegions
                }
            };

            editor.RegionChanged += OnRegionChanged;
            editor.RegionDeleted += OnRegionDeleted;

            imageRegionsCanvas.Children.Add(editor);
        }
        private void OnRegionDeleted(object sender, EventArgs e)
        {
            RegionEditorControl regionControl = (RegionEditorControl)sender;

            this.imageRegionsCanvas.Children.Remove(regionControl);

            var regionBeingDeleted = ((RegionEditorViewModel)regionControl.DataContext).Region;

            ImageViewModel imgViewModel = (ImageViewModel)this.DataContext;

            if (imgViewModel.Image.Regions != null && imgViewModel.Image.Regions.Contains(regionBeingDeleted))
            {
                imgViewModel.DeletedImageRegions.Add(regionBeingDeleted);
            }
            else if (imgViewModel.AddedImageRegions.Contains(regionBeingDeleted))
            {
                imgViewModel.AddedImageRegions.Remove(regionBeingDeleted);
            }

            needsSaving = true;
        }