Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

OnRender ScaleTransform confusion?

$
0
0

Hi,

I have created a new WPF application and within that application I have created a new class called MyElement, which is derived from the FrameworkElement class.

Within the MyElement class I have overridden the following method...

protected override void OnRender(DrawingContext dc)
{
  dc.DrawLine(new Pen(Brushes.Red, 1.0),
    new Point(0.0, 0.0),
    new Point(ActualWidth, ActualHeight));
}

...and within the application XAML code I have written the following...

<Grid><local:MyElement /></Grid>

When I run the application, the application draws a line from the top left to the bottom right as expected.

If I then make the following change to the XAML code...

<local:MyElement RenderTransformOrigin="0.5,0.5"><local:MyElement.RenderTransform><ScaleTransform ScaleY="-1.0" /></local:MyElement.RenderTransform></local:MyElement>

... the application then draws a line from the bottom left to the top right, basically flipping the image along the Y axis (I have done this as I want to count up from bottom to top), which it what I am trying to achieve. However, I want to do this in the code behind, and sadly this is where I am starting to have problems.

So after restoring the XAML code to...

<Grid><local:MyElement /></Grid>

...I made the following changes to the code behind...

protected override void OnRender(DrawingContext dc)
{
  dc.DrawLine(new Pen(Brushes.Blue, 2.0),
    new Point(0.0, 0.0),
    new Point(ActualWidth, ActualHeight));

  RenderTransformOrigin = new Point(0.5, 0.5);
  dc.PushTransform(
    new ScaleTransform(1.0, -1.0, 0.5, 0.5));
}

However, this does not display anything, and would appear that the image is being displayed off-screen!

I then tried the following...

protected override void OnRender(DrawingContext dc)
{
  dc.DrawLine(new Pen(Brushes.Blue, 2.0),
    new Point(0.0, 0.0),
    new Point(ActualWidth, ActualHeight));

  RenderTransformOrigin = new Point(0.5, 0.5);
  ScaleTransform flipTrans =
    new ScaleTransform(1.0,-1.0);
  RenderTransform = flipTrans;
}

This code does work, BUT it is very slow and jerky.

So my question is, how do I mimic the working XAML code in the code behind?

NOTE: The reason that I am not doing it in XAML code is that I want to add the lines to the control dynamically.

Also, if this is not the way to achieve this, then please advice as I have not had much exposure to WPF coding.

Regards

Ron


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>