/// <summary> /// Control loaded event handler. /// </summary> /// <param name="sender">Event sender</param> /// <param name="e">Event arguments</param> private void PolygonTimeBar_Loaded(object sender, RoutedEventArgs e) { //Hack this to fit the existing property but this is not ideal. This control should have its own view model and then //the control can inherit from Catel:UserControl and Catel can inject that VM behind this control. //That VM will have a parent of the VM being used here and then you can walk the ladder if needed. //However VM should be mostly self contained and not need to rely on their parents. They can expose functions that the //parents can interact with when needed. if (DataContext is PolygonEditorViewModel vm) { VM = vm; } }
/// <inheritdoc /> protected override void Initialize() { // Call the base class implementation base.Initialize(); if (ViewModel is PolygonEditorViewModel vm) { // Save off the view model _vm = vm; // Give the view model the editor capabilities vm.EditorCapabilities = _polygonContainer.EditorCapabilities; // Attempting to size the editor to fill 70% of the primary monitor // ReSharper disable once InconsistentNaming const double PercentOfScreen = 0.7; double wfactor = ((SystemParameters.PrimaryScreenWidth * PercentOfScreen) / _polygonContainer.Width); double hfactor = ((SystemParameters.PrimaryScreenHeight * PercentOfScreen) / _polygonContainer.Height); // Take the minimum scale factor between the width and height double factor = Math.Min(wfactor, hfactor); // Calculate the width and height of the canvas using the scale factor and rounding to the next largest pixel vm.CanvasWidth = (int)Math.Ceiling(_polygonContainer.Width * factor); vm.CanvasHeight = (int)Math.Ceiling(_polygonContainer.Height * factor); // Determine the X and Y scale factors double xScaleFactor = 1.0; // Need to guard against dividing by zero when working with 1 pixel width if (_polygonContainer.Width - 1.0 == 0.0) { xScaleFactor = (vm.CanvasWidth - 1.0); } else { xScaleFactor = (vm.CanvasWidth - 1.0) / (_polygonContainer.Width - 1.0); } double yScaleFactor = 1.0; // Need to guard against dividing by zero when working with 1 pixel height if (_polygonContainer.Height - 1.0 == 0.0) { yScaleFactor = (vm.CanvasHeight - 1.0); } else { yScaleFactor = (vm.CanvasHeight - 1.0) / (_polygonContainer.Height - 1.0); } // Give the polygon point converter the scale factor PolygonPointXConverter.XScaleFactor = xScaleFactor; PolygonPointYConverter.YScaleFactor = yScaleFactor; // Give the polygon point converter the Y dimension of the drawing canvas PolygonPointYConverter.BufferHt = _polygonContainer.Height; // If the edit is in time based mode then... if (_polygonContainer.EditorCapabilities.ShowTimeBar) { // Initialize the snapshots with the polygons/lines vm.InitializePolygonSnapshots( _polygonContainer.Polygons, _polygonContainer.PolygonTimes, _polygonContainer.Lines, _polygonContainer.LineTimes, _polygonContainer.Ellipses, _polygonContainer.EllipseTimes); } else { // Give the view model the model polygons and polygon times vm.InitializePolygons(_polygonContainer.Polygons, _polygonContainer.PolygonTimes); // Give the view model the model lines and line times, vm.InitializeLines(_polygonContainer.Lines, _polygonContainer.LineTimes); // Give the view model the model ellipses and ellipse and times vm.InitializeEllipses(_polygonContainer.Ellipses, _polygonContainer.EllipseTimes); } // Initialize the window width to the canvas width Width = vm.CanvasWidth; // If the display element is long and tall don't let the window // get too skinny if (Width < PercentOfScreen * SystemParameters.PrimaryScreenWidth) { Width = SystemParameters.PrimaryScreenWidth * PercentOfScreen; } // If the editor is in time based mode then need to adjust the height for the time bar int timeBarHeight = 0; if (vm.EditorCapabilities.ShowTimeBar) { timeBarHeight = 190; } // Set the height of the window Height = vm.CanvasHeight + timeBarHeight; // If the calculated height is less than the minimum then... const double MinimumHeightPercentage = 0.25; if (Height < MinimumHeightPercentage * SystemParameters.PrimaryScreenHeight) { // Set the height to the minimum Height = MinimumHeightPercentage * SystemParameters.PrimaryScreenHeight; } // Give the view model the width of the window vm.WindowWidth = (int)this.Width; } }