Search code examples
c#wpfcanvas

Canvas zooming in WPF using code behind


Here the scenario is:


I have a canvas with different diagrams drawn on it. Now the requirement is to zoom into the canvas using the code behind either using C# or VB. Moreover I need to place the zoom code in some dll so that i can reuse the same set of code through out my application.

Now my question is how to do this....

I have tried the following code pls have a look..

public MainWindow()
{
    InitializeComponent();

    canvas.MouseEnter += new MouseEventHandler(canvas_MouseEnter);
    canvas.MouseWheel += new MouseWheelEventHandler(canvas_MouseWheel);
}

void canvas_MouseWheel(object sender, MouseWheelEventArgs e)
{
    double height = canvas.ActualHeight;
    double width = canvas.ActualWidth;
    double zoom = e.Delta;
    height += 2;
    width += 2;
    ScaleTransform sc = new ScaleTransform(width, height);
    canvas.LayoutTransform = sc;
    canvas.UpdateLayout();
}

Solution

  • I believe what you are looking for is a zoom behavior. Behaviors are objects that encapsulate some form of interactive behavior. I've seen several examples of "Zoom Behaviors" that you should be able to use for your project.