tags:

views:

47

answers:

2

Hello,

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  A: 

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.

Ray Burns
+1  A: 

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.ColumnDefinitons.Add(new ColumnDefinition());
grid1.ColumnDefinitons.Add(new ColumnDefinition());
grid1.ColumnDefinitons.Add(new ColumnDefinition());

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


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