0

I am using the resize code from this answer which works perfectly when testing but when placed into a real application (with a larger render time) the aspect ratio no is no longer maintained. If you move the mouse slowly or wiggle in certain directions you can make the control distort out of its original aspect ratio and it only seems to happen once in an actual application.

I'm not 100% sure but I wonder if its because it uses ActualHeight and ActualWidth to calculate the current ratio and that those values might not always be up to date.

Some of the related code, the rest is available on the question linked above:

Called on drag

this.CheckAspectRatio(ref dragDeltaHorizontal, ref dragDeltaVertical, item.ActualHeight / item.ActualWidth);

The function itself

private void CheckAspectRatio(ref double? dragDeltaHorizontal, ref double? dragDeltaVertical, double aspectRatio)
{
    double? dragValue = null;
    if (dragDeltaVertical.HasValue && dragDeltaHorizontal.HasValue)
    {
        dragValue = Math.Max(dragDeltaVertical.Value, dragDeltaHorizontal.Value);
    }
    else if (dragDeltaVertical.HasValue)
    {
        dragValue = dragDeltaVertical;
    }
    else if (dragDeltaHorizontal.HasValue)
    {
        dragValue = dragDeltaHorizontal;
    }

    if (dragValue.HasValue)
    {
        dragDeltaVertical = dragValue.Value * aspectRatio;
        dragDeltaHorizontal = dragValue;
    }
}
7
  • the solution seems to be complicated for a simple problem. could you post some sample images showing the resize handles/sides etc. appreciated if you could also share the xaml or c# code used for generating the elements.
    – pushpraj
    Aug 18, 2014 at 10:23
  • Hey, why does it seem complicated? Resizing an arbitary control is not native to WPF and quite a difficult task, unless you know different? :)
    – Chris
    Aug 18, 2014 at 10:26
  • My comment was intended to the answering code in the linked post. By simple I mean to say that, you may consider using UIElement.ManipulationDelta Event instead of DragEvents. also do not forget to set the ManipulationMode otherwise you may not receive the scale values as mentioned in this answer stackoverflow.com/questions/20004620/…
    – pushpraj
    Aug 18, 2014 at 10:51
  • 1
    The ActualHeight and ActualWidth properties are the best to use and the ones most likely to supply the correct values at any given time.
    – Sheridan
    Aug 18, 2014 at 10:53
  • 1
    @Chris why aren't you using ScaleTransform and TranslateTransform? It seems to me it's the perfect way to move and resize controls.
    – CKII
    Aug 18, 2014 at 12:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.