【发布时间】:2017-01-04 14:16:45
【问题描述】:
我正在使用此代码创建一个显示图像和文本的列,但只显示文本,我做错了什么?
DataGridTemplateColumn col1 = new DataGridTemplateColumn(); col1.Header = "我的标题";
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);
【问题讨论】:
-
在 XAML 中做这些事情要容易得多。
标签: wpf image text datagridtemplatecolumn