tags:

views:

1937

answers:

6

Given the following canvas:

<Canvas>
    <Canvas.LayoutTransform>
        <ScaleTransform ScaleX="1" ScaleY="1" CenterX=".5" CenterY=".5" />
    </Canvas.LayoutTransform>
    <Button x:Name="scaleButton" Content="Scale Me" Canvas.Top="10" Canvas.Left="10" />
    <Button x:Name="dontScaleButton" Content="DON'T Scale Me" Canvas.Top="10" Canvas.Left="50" />
</Canvas>

Is it possible to scale 1 button, but not the other when ScaleX and ScaleY changes?

+3  A: 

Not in XAML. You can do this in code by building the reverse transform and applying it to the object you don't want transformed.

If you want to go fancy, you can build a dependency property that you can attach in XAML to any object you don't want to be transformed by any parent transforms. This dependency property will take the transform of the parent, build a reverse transform and apply it to the object it's attached to.

Franci Penov
A: 

@Franci Penov

Not in XAML. You can do this in code by building the reverse transform and applying it to the object you don't want transformed.

By "reverse transform", do you mean that if the Canvas.LayoutTransform.ScaleX increases by x, then Button.LayoutTransform.ScaleX would need to decrease by y? This is what I was thinking about doing, but was really hoping WPF might have something built-in that would help.

Dylan
I think by reverse he means the inverse transform matrix, which in the case of scaling is the reciprocal of the scale factors. So if you are scaling by x, y, you would undo that by scaling by 1/x, 1/y.
fryguybob
@fryguybob is right. by reverse transformation I mean inverse transform matrix.
Franci Penov
A: 

You could also restructure the elements so that the elements you don't want to scale with the Canvas are not actually children of that Canvas.

<Canvas>
    <Canvas>
        <Canvas.LayoutTransform>
            <ScaleTransform ScaleX="1" ScaleY="1" CenterX=".5" CenterY=".5" />
        </Canvas.LayoutTransform>
        <Button x:Name="scaleButton" Content="Scale Me" Canvas.Top="10" Canvas.Left="10" />
    </Canvas>
    <Button x:Name="dontScaleButton" Content="DON'T Scale Me" Canvas.Top="10" Canvas.Left="50" />
</Canvas>
Joel B Fant
A: 

@Joel B Fant

You could also restructure the elements so that the elements you don't want to scale with the Canvas are not actually children of that Canvas.

I don't believe that will work, because I need the button to stay in the same position on the Canvas that is scaling.

Dylan
A: 

I would go the custom Attached property route! should be pretty simple to implement too!

A: 

Hello,,

i was trying to perform a scale transform on a grid, the grid is getting scaled but the elements in it( the text inside the text box and the content in the button is not, can anyone throw some light on this)

            </Grid.RenderTransform>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>                 
            </Grid.RowDefinitions>
            <TextBlock Grid.Row="0"  Text="This is a pop up">
                                </TextBlock>
            <Button Grid.Row="1" Content="close" MouseLeftButtonDown="Button_MouseLeftButtonDown"/>
        </Grid>
rishi