user955822
user955822

Reputation:

Setting position of Image control relative to screen

I am currently learning C# in my spare time but have become stuck. I am trying to move an Image control to a random position (Horizontal) on the screen, this is done based on the screen size and the size of the image control itself. Is it possible to set a Image.X and Image.Y position within C# and WPF?

Upvotes: 0

Views: 1331

Answers (1)

McGarnagle
McGarnagle

Reputation: 102793

You'll probably want to use a Canvas panel for this, as it allows you to position children at X,Y coordinates.

<Canvas>
    <Image x:Name="myImage" Canvas.Top="100" Canvas.Left="200" />
</Canvas>

The Canvas.X and Canvas.Y are attached properties. You can set them from code-behind like this:

myImage.SetValue(Canvas.Left, 200);
myImage.SetValue(Canvas.Top, 400);

Upvotes: 1

Related Questions