Tuesday, June 23, 2009

Mirroring Text in WPF

A friend of mine is working on a project that needs an application that mirrors the text. After fiddling around with transformations in Delphi and Winforms I wondered how WPF might handle this. After fiddling around for a few minutes it was obscenely easy... He tells me that some software that do this cost a few hundred dollars in the area he's trying to enter. With WPF (using Delphi Prism in my case) it took two lines of code:


method Window1.button1_Click(sender: System.Object; e: System.Windows.RoutedEventArgs);
begin
richTextBox1.RenderTransformOrigin := new Point(0.5, 0.5);
richTextBox1.RenderTransform := new ScaleTransform(-1, 1, 1, 1);
end;


The project is here if you need to do the same thing. Not sure I could ever charge for something that only takes a couple lines of code. :-) I still have a long way to go with WPF, though, because from the MSDN documentation I thought I could just do it in XAML without needing code. This code doesn't work, though: Thanks to anonymous I see what I did wrong and it works great now. The following XAML code will give you a RichTextBox that reverses its text by default. Should work fine with other controls as well:


<Grid>
<RichTextBox margin="12,41,12,12" name="richTextBox1">
<RichTextBox.RenderTransformOrigin>
<Point x="0.5" y="0.5" />
</RichTextBox.RenderTransformOrigin>
<RichTextBox.RenderTransform >
<ScaleTransform ScaleX="-1" />
</RichTextBox.RenderTransform>
</RichTextBox >
</Grid>


Seems like I have it the same as the ScaleTransform documentation here. Any ideas appreciated...


About The Author

Ron Grove draws on over ten years of training, network administration and development experience. He loves to work with new technology and see how that technology can be best utilized by his clients. You can find him through his company Evanoah, LLC and his LinkedIn profile is here.

1 comments:

Anonymous said...

You don't need to set the ScaleTransform Center as you set the RenderTransformOrigin of your control. Btw, to mirror the text, you should write, in XAML :

ScaleTransform ScaleX="-1" ScaleY="1"

Regards,

Roland Tomczak

Post a Comment