从C#在TableCell中插入图像

时间:2014-03-13 10:28:12

标签: c# wpf bitmapimage tablecell

我是.NET(WPF)中的新手,我在某些方面陷入困境,我猜它真的很琐碎。

我正在使用C#代码创建FlowDocumentTableenter image description here

当我即将创建行时,我需要在单元格中插入图像"预测"

// ******* TODAY ROW ******
oTable.RowGroups[0].Rows.Add(new TableRow());
currentRow = oTable.RowGroups[0].Rows[1];

//Configure the row layout
currentRow.Background = System.Windows.Media.Brushes.White;
currentRow.Foreground = System.Windows.Media.Brushes.Navy;

//Add the dayin the first cell
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Today"))));

//Add the image in the second cell
BitmapImage bmp0 = new BitmapImage();
System.Windows.Controls.Image img0 = new System.Windows.Controls.Image();
bmp0.BeginInit();
bmp0.UriSource = new Uri("weather/cloudy.gif", UriKind.Relative);
bmp0.EndInit();
Paragraph oParagraph0 = new Paragraph();
oParagraph0.Background = new ImageBrush(bmp0);
currentRow.Cells.Add(new TableCell(oParagraph0));

正如您所看到的,现在我正在设置图像" bmp0"就像段落的背景一样...... 如何将其设置为普通图像(不将其调整为背景)?

//Add the image in the second cell
BitmapImage bmp0 = new BitmapImage();
System.Windows.Controls.Image img0 = new System.Windows.Controls.Image();
bmp0.BeginInit();
bmp0.UriSource = new Uri("weather/cloudy.gif", UriKind.Relative);
bmp0.EndInit();
Image image = new Image();
image.Source = bmp1;
BlockUIContainer block= new BlockUIContainer(image);
currentRow.Cells.Add(new TableCell(block));

1 个答案:

答案 0 :(得分:2)

您可以将图片控件img0放在BlockUIContainer(或InlineUIContainer)中:

var bitmap = new BitmapImage(new Uri("weather/cloudy.gif", UriKind.Relative));
var image = new Image { Source = bitmap };
var block = new BlockUIContainer(image);

currentRow.Cells.Add(new TableCell(block));