项目面板的WPF RenderTargetBitmap但省略了某些元素

时间:2011-03-08 13:39:53

标签: wpf wpf-controls

我正在使用RenderTargetBitmap.Render(面板)来捕捉项目面板。

但是我想省略某些元素(将OmitWhileRenderingToBitmap属性设置为false的自定义项)。

是否有一些简单的方法可以做到这一点,从项目面板中删除项目,拍摄快照然后再添加它们?

编辑:基本上这是我的自定义列表框类中的关键位。删除过滤器位,一切都按预期工作。

    protected void UpdatePanelBitmapSource()
    {
        if (this._itemsPanel != null) //_itemsPanel here is the canvas within the ListBox. Not rendering the ListBox for a reason.
        {
            Matrix m = PresentationSource.FromVisual(this._itemsPanel).CompositionTarget.TransformToDevice;

            var existingFilter = this.Items.Filter;

            // Add filter
            this.Items.Filter = item =>
            {
                var mgvListBoxItem = this.ItemContainerGenerator.ContainerFromItem(item) as MGVListBoxItem;

                if (mgvListBoxItem != null)
                    return !mgvListBoxItem.IsEditable;
                else
                    return false;
            };

            var panelBitmap = new RenderTargetBitmap(
                (int)Math.Floor(this._itemsPanel.ActualWidth),
                (int)Math.Floor(this._itemsPanel.ActualHeight),
                m.M11 * 96.0,
                m.M22 * 96.0,
                PixelFormats.Default);
            panelBitmap.Render(this._itemsPanel);

            // Remove filter
            this.Items.Filter = existingFilter;

            this.PanelBitmapSource = panelBitmap;
        }
    }

2 个答案:

答案 0 :(得分:2)

只需应用过滤器:

// Add filter
yourItemsControl.Items.Filter = o =>
    {
        YourType item = (YourType)o;
        return !item.OmitWhileRenderingToBitmap;
    };


// Render to bitmap
// ...

// Remove filter
yourItemsControl.Items.Filter = null;

答案 1 :(得分:0)

最后我做了类似的事情

        protected void OnUpdatePanelBitmapSource()
    {
        if (this._itemsPanel != null)
        {
            Matrix m = PresentationSource.FromVisual(this._itemsPanel).CompositionTarget.TransformToDevice;

            var hiddenItems = new List<MGVListBoxItem>();

            foreach(var item in this.Items)
            {
                var mgvListBoxItem = this.ItemContainerGenerator.ContainerFromItem(item) as MGVListBoxItem;
                if (mgvListBoxItem != null && mgvListBoxItem.IsEditable && mgvListBoxItem.IsVisible)
                {
                    mgvListBoxItem.Visibility = Visibility.Hidden;
                    hiddenItems.Add(mgvListBoxItem);
                }
            }

            var panelBitmap = new RenderTargetBitmap(
                (int)Math.Floor(this._itemsPanel.ActualWidth),
                (int)Math.Floor(this._itemsPanel.ActualHeight),
                m.M11 * 96.0,
                m.M22 * 96.0,
                PixelFormats.Default);

            panelBitmap.Render(this._itemsPanel);

            foreach (var mgvListBoxItem in hiddenItems)
            {
                mgvListBoxItem.Visibility = Visibility.Visible;
            }

            this.PanelBitmapSource = panelBitmap;
        }
    }