Tuesday, November 6, 2007

Zooming in Silverlight

There is no direct support to zoom a control like in WPF.But we could work around using RenderTransform.

  1. Add a ScaleTransform in the RenderTransform section
  2. Manipulate the X & Y values of ScaleTransform through code behind.ie in a Click event

Detailed steps

  1. Create a new SilverlightJavascript application ( Silverlight 1.0)
  2. Then you can see a Canvas which looks like a Button.Add 2 more canvases for ZoomIn and ZoomOut
    <Canvas Name="ZoomIn" Width="100" Height="50" Canvas.Top="370" >
    <TextBlock Text="Click me to ZoomIn"/>
    </Canvas>
    <Canvas Name="ZoomOut" Width="100" Height="50" Canvas.Top="370" Canvas.Left="150" >
    <TextBlock Text="Click me to ZoomOut"/>
    </Canvas>

  3. Add event handlers on LeftMouseDown in Scene.XAML.js file
    this.ZoomIn = rootElement.children.getItem(1);
    this.ZoomIn.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.mouseDownZoomIn));

    this.ZoomOut = rootElement.children.getItem(2);
    this.ZoomOut.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.mouseDownZoomOut));

  4. Write Handlers
    mouseDownZoomIn:function(sender,args)
    {
    var transform=this.button.RenderTransform;
    transform.ScaleX=transform.ScaleX+1;
    transform.ScaleY=transform.ScaleY+1;
    },
    mouseDownZoomOut:function(sender,args)
    {
    var transform=this.button.RenderTransform;
    if(transform.ScaleX >1){
    transform.ScaleX=transform.ScaleX-1;
    transform.ScaleY=transform.ScaleY-1;
    }




A sample is located here

No comments: