views:

374

answers:

2

Hi all,

I've got a button style that I've been developing in WPF, as stated in this question. Another thing I'd like to do with this style is to have the button shrink just a little bit, to make it appear like it's getting clicked as it's getting clicked. Right now, the transform code looks like:

<Trigger Property="IsPressed" Value="True">
    <Setter Property="RenderTransform">
        <Setter.Value>
            <TransformGroup>
                <ScaleTransform ScaleX="0.98" ScaleY="0.98"/>
                <SkewTransform AngleX="0" AngleY="0"/>
                <RotateTransform Angle="0"/>
                <TranslateTransform X="0" Y="0"/>
            </TransformGroup>
        </Setter.Value>
    </Setter>
    <Setter Property="Button.BitmapEffect">
        <Setter.Value>
            <OuterGlowBitmapEffect GlowColor="Green" GlowSize="10"></OuterGlowBitmapEffect>
        </Setter.Value>                                
    </Setter>
</Trigger>

So, that's exciting. Problem is, the scale transform scales the image associated with the button to the upper left of the area where the button is (ie, it's rescaling to the 0,0 coordinate of the button, or at least, that's what I assume).

My understanding of the TranslateTransform is that it's in pixel coordinates, not in coordinates relative to the size of the object. If I put in a TranslateTransform of 0.01, 0.01, then it will move the button by 0.01 pixels in both x and y, rather than 0.01*sizeof(imagedimension) pixels in each direction. How can I get that relative transform, and how can I have it happen as a style, ie, so I don't have to do different math for every different button size I've got?

+1  A: 

You need to set the RenderTransformOrigin on the Button to 0.5 by 0.5:

RenderTransformOrigin="0.5,0.5"
Josh G
You can also set the CenterX and CenterY values on the ScaleTransform if you want, but that doesn't work for buttons of all sizes.
Josh G
thanks! I had to give the answer to gcores, because he gave me the full code to fire the fruiton torpedoes and make it go that way.
mmr
That's fine... I thought about putting the origin in the setter, but thought that you may not want it there. If you want the transform origin to affect ALL transforms, not just the triggered one, you will need to set the origin on the button itself.
Josh G
+2  A: 

To scale through the center you can add in your trigger:

<Setter Property="RenderTransformOrigin" Value=".5,.5"/>
gcores
that got it, awesome!
mmr