Hi. I am trying to perform a TranslateTransform on an Image using this code:

double to;
int increment = 2;

TranslateTransform tt = new TranslateTransform();
background.RenderTransform = tt;

to = Canvas.GetTop(background) + increment;
DoubleAnimation da = new DoubleAnimation(to, TimeSpan.FromSeconds(0));
Canvas.SetTop(background, to);
tt.BeginAnimation(TranslateTransform.YProperty, da);

This works when the Image exists in a Window:

<Window x:Class="trpgwpf1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" ResizeMode="CanMinimize" KeyDown="WindowKeyDown">
    <Canvas Name="canvas">
        <Image Name="background" Stretch="UniformToFill" Source="Images\test.png" />
    </Canvas>
</Window>

But nothing happens when I try to transform an Image on a Page:

<Page x:Class="trpgwpf1.Page1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Page1">
    <Canvas Name="canvas">
        <Image Name="background" Stretch="UniformToFill" Source="Images\test.png" />
    </Canvas>
</Page>

Does anyone know why? I can't find a reference to anything like this anywhere.

I think you want to use the transform on the backgrounds' Canvas.TopProperty.

Like this:

            double To = -1000;
            double From = (double)Background.GetValue(Canvas.TopProperty);

            DoubleAnimation da = new DoubleAnimation(From, To, TimeSpan.FromMilliseconds(10));

            Background.BeginAnimation(Canvas.TopProperty, da);

The problem, i think you will run into, however, is that you have the UniformToFill set and so your image might stretch/.

Mark A. Malo

Thanks, unfortunately I'm still not seeing the animation with your code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.