How to set ScaleTransform.CenterX in code?

  • Thread starter Thread starter DeanB
  • Start date Start date
D

DeanB

Anyone know how to set the CenterX property of a ScaleTransform (on an
Image object) NOT using XAML?

Here is how I do this in XAML:

<Image Grid.Row="0"
Name="image1"
MouseMove="image1_MouseMove"
MouseDown="image1_MouseDown"
MouseUp ="image1_MouseUp">
<Image.RenderTransform>
<ScaleTransform ScaleX="2" ScaleY="2" CenterX="250"
CenterY="200">
</ScaleTransform>
</Image.RenderTransform>
</Image>

The problem is, I need to set CenterX and CenterY dynamically to the
center of the window, which can be resized at any time by the user.

Thanks
-Dean
 
Granted, I'm new to this whole WPF thing.  And I haven't bothered to  
actually _try_ this (but then, you didn't post anycodeeither, so :p ).  
But it seems to me that if you just create a new ScaleTransform with the  
center where you want it, and then assign that to the RenderTransform  
property of the UIElement you are trying to center, that would do it.

If it's anything like the regular Drawing namespace stuff, setting the  
center X/Y on the transform you get from the RenderTransform property  
won't work.  You need to reassign the transform back to the property in  
order for the object being transformed to actually be updated (in the  
Drawing namespace, the transform is on the Graphics object, but same  
difference otherwise :) ).

Pete

Ok, this rings a bell with something I read yesterday (somewhere!), I
will look into it further. It seemed a round-the-houses kind of method
compared to the xaml version.

Thanks
-Dean
 
Anyone know how tosetthe CenterX property of a ScaleTransform (on an
If you've already got a transform defined in XAML (as in your example) then
you can change its properties by code like:
((ScaleTransform)image1.RenderTransform).CenterX = newValue;

You could also use databinding to bind the centerX and CenterY properties to
the window's ActualWidth and ActualHeight, though you'd need to use a
ValueConverter to divide the values by 2 (or use Kent Boogaart's
ExpressionConverter - see
http://www.codeplex.com/wpfconverters/Wiki/View.aspx?title=User Documentation&referringTitle=Home).

Chris Jobson
 
If you've already got a transform defined in XAML (as in your example) then
you can change its properties by code like:
((ScaleTransform)image1.RenderTransform).CenterX = newValue;

You could also use databinding to bind the centerX and CenterY properties to
the window's ActualWidth and ActualHeight, though you'd need to use a
ValueConverter to divide the values by 2 (or use Kent Boogaart's
ExpressionConverter - seehttp://www.codeplex.com/wpfconverters/Wiki/View.aspx?title=User%20Doc...).

Chris Jobson

Thanks Chris that works well.

-Dean
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top