0

It's simple enough to define an image in xaml and move it around, but how would I do this programmaticly? I define my Image like this:

System.Windows.Controls.Image imgpanel = new System.Windows.Controls.Image();
imgpanel.Source = loadBitmap(capwin);

And I'd like to be able to set it on my window. How would I got about this?

1
  • I've found that I can place the image on a grid ( grid1.Children.Add(imgpanel); ), but the add method doesn't let me specify a position for it. Commented Nov 16, 2009 at 1:36

2 Answers 2

2

Since you're using a grid, if your desired location for the image is (x,y), use this code:

imgpanel.Margin = new Thickness(x, y, 0, 0);

Adjusting the margin of the image relative to the grid will cause the image to move around on the grid.

This also works if the image is added directly to the window.

Sign up to request clarification or add additional context in comments.

Comments

1

for specifying position inside a grid in C# you can write like this,

i am assuming you want to add image in 2nd row and 3rd column

grid1.ColumnDefinitions.Add(new ColumnDefinitions());
grid1.ColumnDefinitions.Add(new ColumnDefinitions());
grid1.ColumnDefinitions.Add(new ColumnDefinitions());

grid1.RowDefinitons.Add(new RowDefinition());
grid1.RowDefinitons.Add(new RowDefinition());


imgpanel.SetValue(Grid.RowDefinitionProperty, 1);
imgpanel.SetValue(Grid.ColumnDefinitionProperty, 2);
grid1.Children.Add(imgpanel);

Comments

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.