I am using this code to create a column that shows image and text, but only the text is appearing, what am I doing wrong?
DataGridTemplateColumn col1 = new DataGridTemplateColumn(); col1.Header = "MyHeader";
FrameworkElementFactory factoryStackPanel = new FrameworkElementFactory(typeof(System.Windows.Controls.StackPanel));
factoryStackPanel.SetValue(System.Windows.Controls.StackPanel.OrientationProperty, Orientation.Vertical);
FrameworkElementFactory factoryTextBlock = new FrameworkElementFactory(typeof(System.Windows.Controls.TextBlock));
Binding bindTextBlock = new Binding("[" + i + "]");
factoryTextBlock.SetValue(System.Windows.Controls.TextBlock.TextProperty, bindTextBlock);
factoryTextBlock.SetValue(System.Windows.Controls.TextBlock.TextWrappingProperty, TextWrapping.Wrap);
factoryTextBlock.SetValue(System.Windows.Controls.TextBlock.HorizontalAlignmentProperty, HorizontalAlignment.Center);
FrameworkElementFactory factoryImage = new FrameworkElementFactory(typeof(System.Windows.Controls.Image));
Binding bindImage = new Binding("http://www.pgn.co.id/images/modules/logo_pgn.png");
factoryImage.SetValue(System.Windows.Controls.Image.SourceProperty, bindImage);
factoryStackPanel.AppendChild(factoryImage);
factoryStackPanel.AppendChild(factoryTextBlock);
DataTemplate cellTemplate = new DataTemplate() { VisualTree = factoryStackPanel };
col1.CellTemplate = cellTemplate;
gridViewItens.Columns.Add(col1);
Like Ed Plunkett said, it's much more clean to do it in XAML. Assuming that your
DataGrid
is bound to anObservableCollection
of some items, and you item class has properties likeMyText
andMyImage
, you could do something like: