2011-03-08 106 views
0

我正在使用RenderTargetBitmap.Render(面板)拍攝項目面板。項目面板的WPF RenderTargetBitmap,但省略了某些元素

但是我想省略某些元素(自定義項目與說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; 
     } 
    } 

回答

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; 
     } 
    } 
2

只是應用濾鏡:

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


// Render to bitmap 
// ... 

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

我不知道爲什麼,但這樣是行不通的。我添加了兩個項目,其中一個是忽略標誌,另一個是關閉項目。我可以在調試器中看到它的一切正常。但它呈現的只是ItemsControl背景,沒有任何項目。有什麼想法嗎? – NVM 2011-03-09 11:02:41

+0

即使是陌生人,過濾器也適用於物品控制,但渲染圖像中沒有任何項目。 – NVM 2011-03-09 12:14:16

+0

@NVM,你能編輯你的問題來添加你的實際代碼嗎? – 2011-03-09 12:42:24