UWP - Changing Matrix of a MatrixTransform doesn't result in a change of the Transform

203 Views Asked by At

I want to set zoom and offset of an element using its RenderTransform.

To do this, the following code works:

public static void SetMatrixTransform(this UIElement elem, double zoom, double offsetX, double offsetY)
{
    elem.RenderTransform = new MatrixTransform
    {
        Matrix = new Matrix(zoom, 0, 0, zoom, offsetX, offsetY)
    };
}

But then I want to improve my performance, by avoiding re-creating every time the MatrixTransform, since I just need to set its values.

So I came out with the following code:

public static void SetSmartMatrixTransform(this UIElement elem, double zoom, double offsetX, double offsetY)
{
    if (elem == null)
        return;

    if (!(elem.RenderTransform is MatrixTransform))
    {
        elem.RenderTransform = new MatrixTransform
        {
            Matrix = new Matrix(zoom, 0, 0, zoom, offsetX, offsetY)
        };
    }
    else
    {
        ((MatrixTransform)elem.RenderTransform).Matrix = new Matrix(zoom, 0, 0, zoom, offsetX, offsetY);
    }
}

Unfortunately, when I hit the else branch, the Matrix property is not updated! i.e. the elem.RenderTransform keeps the same value that it had at the beginning of the method.

Is it possible to just change the Matrix without re-creating every time the MatrixTransform?

If yes, how can I do?

0

There are 0 best solutions below