Search code examples
c#.netwpfzoomingviewbox

WPF ViewBox zoom/size changed event


I would like to detect changes of the zoom or size of the ViewBox so I can apply some custom logic on the controls inside of it.

Is it possible to do?


Solution

  • Try this code:

    private void Viewbox_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        double initialWidth = 509.0;
    
        if (e.PreviousSize.Width > 0)
        {
            var factor = e.NewSize.Width / initialWidth;
            this.Title = (factor * 100).ToString();
        }
    }
    

    The value 509.0 is the width of my initial Window. Every time you resize the window, I calculate the % between the new width and the initial one. I suppose that you need to subscribe MouseLeftButtonUp to detect the end of each window resizing, so you can save the new width.