Venkateswara Rao
Venkateswara Rao

Reputation: 153

Scaling in XAML

I changed my WPF canvas origin to center of the screen using the below xaml code.

<Canvas.RenderTransform>
    <TransformGroup>
        <ScaleTransform ScaleX="2" ScaleY="2"/>
        <TranslateTransform
            X="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType=Canvas}}"
            Y="{Binding ActualHeight, RelativeSource={RelativeSource AncestorType=Canvas}}"/>
        <ScaleTransform ScaleX="0.5" ScaleY="0.5"/>
    </TransformGroup>
</Canvas.RenderTransform>

I used scale transform in above xaml code to achieve that. But now, I was unable to apply scale transform any more on my canvas to resize the contents of canvas (lines and polygons).

Please help me resolve this issue. Thanks in advance.

Upvotes: 1

Views: 675

Answers (1)

Clemens
Clemens

Reputation: 128013

A much simpler way to "center" the coordinate system origin of a Canvas would be to put it in the bottom-right cell of a 2x2 Grid. Since the default value of the Canvas' ClipToBounds property is false, elements at negative coordinates are still visible.

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Canvas Grid.Column="1" Grid.Row="1">
        ...
    </Canvas>
</Grid>

Upvotes: 2

Related Questions