1

我有一个列表框,它的 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");
        }
4

3 回答 3

1

这段代码对我来说工作正常,我没有尝试打印图像,因为我现在没有打印机,但图像在我创建的测试应用程序中正确呈现

var listBoxItem = listBox.ItemContainerGenerator.ContainerFromIndex(0) as ListBoxItem;
            RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.HighQuality);
            RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap((int)listBoxItem.ActualWidth,
                                                                           (int)listBoxItem.ActualHeight, 96, 96,
                                                                           PixelFormats.Pbgra32);
            renderTargetBitmap.Render(listBoxItem);
            image.Source = renderTargetBitmap;
            image.Width = listBoxItem.ActualWidth;
            image.Height = listBoxItem.ActualHeight;
于 2012-08-10T14:54:01.967 回答
0

我刚刚回答了一分钟:在网格背景显示一条线

使用 Visualbrush 创建一个画笔,然后简单地创建一个 Drawingcontext 并使用 Visualbrush 绘制一个矩形作为示例

于 2012-08-10T14:00:05.230 回答
0

我没有评论的声誉 - 所以要增加 Dtex 的答案:

ListBoxItems 似乎对 RenderTargetBitmap 的行为很奇怪。我能够通过呈现 ListBoxItem 本身的直接子项来解决该问题,如下所示:

var itemChildVisual = VisualTreeHelper.GetChild(listBoxItem,0) as Visual;
renderTargetBitmap.Render(itemChildVisual);

这对我来说非常有效。

希望这可以帮助!

于 2014-03-21T23:25:27.183 回答