Vivek Saurav
Vivek Saurav

Reputation: 2275

How to Restrict canvas size in wpf when Zoomed

I am designing a Wpf in visual studio 2010 Application which is implementing zoom feature in Canvas. The Event is:

  < canvas MouseWheel="Canvas_MouseWheel" />

and the backend in xaml.cs is:

  const double ScaleRate = 1.1;

  private void Canvas_MouseWheel(object sender, MouseWheelEventArgs e)
        {

            if (e.Delta > 0)
            {
                st.ScaleX *= ScaleRate;
                st.ScaleY *= ScaleRate;
            }
            else
            {
                st.ScaleX /= ScaleRate;
                st.ScaleY /= ScaleRate;
            }
        } 

The problem is when i am zooming the figure in canvas ,the whole canvas is expanding and taking the whole space which is undesired.Because i only want zoom in canvas not to expand it and also i have defined max height and max width as well.

Kindly Help me with This.

Upvotes: 0

Views: 445

Answers (1)

basarat
basarat

Reputation: 276255

Perhaps:

 private void Canvas_MouseWheel(object sender, MouseWheelEventArgs e)
    {
        double maxScale = 2.0;

        if (e.Delta > 0)
        {
            st.ScaleX *= ScaleRate;
            st.ScaleY *= ScaleRate;
        }
        else
        {
            st.ScaleX /= ScaleRate;
            st.ScaleY /= ScaleRate;
        }

        if(st.ScaleX > maxScale)
        {
            st.ScaleX = maxScale;
        }

        if(st.ScaleY > maxScale)
        {
            st.ScaleY = maxScale;
        }

    } 

Upvotes: 1

Related Questions