我有一个列表框,它的 ItemsSource 是绑定的。无论如何将每个单独的列表框项目转换为运行时在屏幕上看到的图像?
编辑:
我现在有这个,但它似乎不起作用......
ListBoxItem item = new ListBoxItem();
item = (ListBoxItem)(lb1.ItemContainerGenerator.ContainerFromIndex(0));
//itempres = item.Content as ItemsPresenter;
//double width = itempres.ActualWidth;
//double height = itempres.ActualHeight;
double width = item.Width;
double height = item.Height;
//RenderTargetBitmap bmpCopied = new RenderTargetBitmap((int)Math.Round(width), (int)Math.Round(height), 100, 100, PixelFormats.Default);
RenderTargetBitmap bmpCopied = new RenderTargetBitmap(100, 500, 100, 100, PixelFormats.Default);
DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext drawingContext = drawingVisual.RenderOpen())
{
VisualBrush visualBrush = new VisualBrush(item);
drawingContext.DrawRectangle(visualBrush, null, new Rect(new Point(), new Size(width, height)));
}
bmpCopied.Render(drawingVisual);
displayImage = new Image();
displayImage.Source = bmpCopied;
fd.Blocks.Add(new BlockUIContainer(displayImage));
//ListBox lbNew = new ListBox();
//lbNew.Style = lb1.Style;
//lbNew.ItemsSource = lb1.ItemsSource;
//lbNew.ItemContainerStyle = lb1.ItemContainerStyle;
//lbNew.ItemTemplateSelector = lb1.ItemTemplateSelector;
//UIElement elementAll = lbNew as UIElement;
//fd.Blocks.Clear();
//fd.Blocks.Add(new BlockUIContainer(elementAll));
//}
PrintDialog pd = new PrintDialog();
if (pd.ShowDialog() == true)
{
fd.PageHeight = pd.PrintableAreaHeight;
fd.PageWidth = pd.PrintableAreaWidth;
fd.PagePadding = new Thickness(96);
IDocumentPaginatorSource dps = fd;
pd.PrintDocument(dps.DocumentPaginator, "flow doc");
}