0

I have a XAML page with this body:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
</Grid>

Now I want to add following controls in the Code Behind into the ContentPanel

    <ViewportControl x:Name="viewport" ManipulationStarted="OnManipulationStarted" ManipulationDelta="OnManipulationDelta" ManipulationCompleted="OnManipulationCompleted" ViewportChanged="viewport_ViewportChanged">
        <Canvas x:Name="canvas">
            <Image x:Name="TestImage" RenderTransformOrigin="0,0" CacheMode="BitmapCache" ImageOpened="OnImageOpened">
                <Image.RenderTransform>
                    <ScaleTransform x:Name="xform"/>
                </Image.RenderTransform>
            </Image>
        </Canvas>
    </ViewportControl>

Is there a way to do this by code?

7
  • 1
    Is this what you're looking for? Commented May 16, 2014 at 12:45
  • try this SO answer if it helps you Commented May 16, 2014 at 12:46
  • @Leparevilo, which bits are having problem with? Commented May 16, 2014 at 12:48
  • @Leparevilo tell us what you have tried. Did you even google this? A simple search turns up lots of help. Try googling "wpf add control from code" Commented May 16, 2014 at 12:58
  • I have Trouble, "converting" the XAML-code to C#-code. Creating the ViewportControl is OK. Creating the Canvasis OK. But creating the Imageand it´s "transform-stuff" - here I´m unable to code this. Commented May 16, 2014 at 13:08

1 Answer 1

0

Since

Creating the ViewportControl is OK. Creating the Canvas is OK. But creating the Image and it's "transform-stuff" - here I'm unable to code this

and you don't need to register names you can try this to create Image:

var img = new Image
{
    RenderTransformOrigin = new Point(0,0),
    CacheMode = new BitmapCache(),
    RenderTransform = new ScaleTransform()
};
img.ImageOpened += OnImageOpened;

//and you add it to Canvas
Canvas canvas = new Canvas();
canvas.Children.Add(img);
Sign up to request clarification or add additional context in comments.

2 Comments

This seems to work. Can you finally help me, to add the canvas to the ViewportControl?
VieportControl is a ContentControl so setting the Content like viewPortControl.Content = canvas should work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.