1 Replies - 8167 Views - Last Post: 02 October 2012 - 04:43 PM Rate Topic: -----

#1 rarment  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 27-July 12

Trying to resize and move a canvas with code behind

Posted 07 August 2012 - 06:43 AM

I'm working on a DXF file viewer, and I can draw shapes and everything to the canvas with no problem. My current problem is that I want to be able to resize the canvas to the extents of the DXF drawing and then view just that part of the drawing on the canvas. The issue is that if someone just draws in the upper right hand corner of the drawing, then there would be a lot of wasted space. Where as if I just viewed the stuff in the corner everything would be bigger and no wasted space.

I can read the extents from the drawing and resize the canvas, but I'm having trouble moving... well I'm not sure about this part. Should I be moving the origin or the canvas? I'm new to WPF and C#, so I really need some help or direction. If this doesn't make sense, let me know and I will try to clarify.

Here is the XAML I have, really basic...


<Window x:Class="DXF.ViewerWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="650" Width="650">
    <Grid Name="testGrid">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Button Content="Open DXF" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Click="Button_Click_1"/>
        <Canvas Name="mainCanvas" Grid.Column="1" HorizontalAlignment="Stretch" Grid.Row="1" VerticalAlignment="Stretch"/>

    </Grid>
</Window>



And the code I am using to resize the canvas...
            DXFFile dxfFile = new DXFFile(source);

            //Get the extents from the dxf file
            Point canvasMax = dxfFile.getCanvasMax();
            Point canvasMin = dxfFile.getCanvasMin();

            //Set the canvas size as the difference between the extents min and max
            mainCanvas.Width = (canvasMax.X - canvasMin.X);
            mainCanvas.Height = (canvasMax.Y - canvasMin.Y);



Is This A Good Question/Topic? 0
  • +

Replies To: Trying to resize and move a canvas with code behind

#2 UNIcoder  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 30-September 12

Re: Trying to resize and move a canvas with code behind

Posted 02 October 2012 - 04:43 PM

The trick and the solution is in:

VerticalAlignment="";
HorizontalAlignment="";
Width="";
Heigth="";
Grid.Column="";
Grid.Row="";



Play around with it and you will see the solution.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1