开发者

Create and place an Image in WPF

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 lik开发者_开发知识库e to be able to set it on my window. How would I got about this?


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.


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜