2

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?

NoWar
  • 36,338
  • 80
  • 323
  • 498

2 Answers2

1

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.

Igor Damiani
  • 1,897
  • 9
  • 12
0

Is it possible for you to subscribe the SizeChanged event?

<Viewbox SizeChanged="Viewbox_SizeChanged">

</Viewbox>

And in the code-behind you simply put:

private void Viewbox_SizeChanged(object sender, SizeChangedEventArgs e)
{

}
Igor Damiani
  • 1,897
  • 9
  • 12
  • OK. Would you mind to clarify if the SizeChanged event has something with zoom changes when the ViewBox's size changes, please. Let's say that initial state of ViewBox is 100% and now we change size of ViewBox. Can we use SizeChanged event in order to calculate % of ViewBox zoom changes? – NoWar Jul 22 '16 at 13:38
  • 1
    @Dimi, try it out and see for yourself. `e.PreviousSize.Height / e.NewSize.Height` – Meloviz Jul 22 '16 at 14:01
  • mmmhhhh, you need to save in a field the value of your initial size. I add a new answer to post some code – Igor Damiani Jul 22 '16 at 14:45