I’m trying to dynamically place content with one of the items being an image. I can get the image to load to the grid, but while the labels all align nicely, the image appears in the middle of the screen. I don’t believe I’m doing this right, I’m wondering if I should be using a list? But, I’m open for suggestions. Currently I’m writing the labels with enough left margin space to separate them, but when the image renders it appears in the middle of the window.
...
Uri imguri = new Uri("/MyName;Component/Resources/myimage.png", UriKind.RelativeOrAbsolute);
BitmapImage ni = new BitmapImage(imguri);
gridEvents.Children.Add(new Label { Content = "Travel:", Margin = new Thickness(300, 0, 0, 0), FontSize = 18 });
gridEvents.Children.Add(new Image() { Source = ni, Height = 15, Width = 15 });
...
I believe the problem is similar to how I’m forcing the labels position with a hard coded margin. I’m new to wpf, so if you know of a better way – I’m ready to learn.
Image appears in the middle of the grid because you didn’t set any positioning properties, like margin or align. Position in the center of the grid is just default behavior.
Actually you’d better use ItemsControl or ListView. To better understand ItemsControl read this series of articles.