Canvas with BitmapCache - Render after removing child

*爱你&永不变心* 提交于 2021-01-28 17:36:10

问题


I have a user control that contains the following elements:

<UserControl
 ...>
  <ScrollViewer Name="LayoutScroll" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">

    <Grid Name="MainGrid">
      <Canvas x:Name="LayerObjects" Panel.ZIndex="2"
       Width="{Binding ActualWidth,ElementName=MainGrid}" Height="{Binding ActualHeight, ElementName=MainGrid}">
            <Canvas.CacheMode>
                <BitmapCache 
                EnableClearType="False" 
                SnapsToDevicePixels="False"
                RenderAtScale="2"/>
            </Canvas.CacheMode>
        </Canvas>
    </Grid>

  </ScrollViewer>
</UserControl>

I use this user control to have multiple shapes drawn into it. The drawing is working perfectly and the removing was working as well (I dont know what have I changed, just cant figure it out). Every time I want to remove an element from this canvas I would just simply do:

LayerObject.Children.Remove(shape);

Now, when I do that the shape is not visually removed from the canvas. It just stays there. When I move the window or zoom into the canvas the shape dissapears. So, my question is, is there any way to "render" the canvas as soon as the element is removed ?

What have I tried so far (without success):

  • oCanvas.UpdateLayout();
  • oCanvas.InvalidateVisual();
  • Verify if the removing action is running on the dispatcher (It is).
  • Works if CacheMode is removed but loses performance when handling multiple shapes.

回答1:


I've resolved this by removing the CacheMode before changing the Canvas children and then put it back on.

CacheMode oTempCM = oCanvas.CacheMode;
oCanvas.CacheMode = null;
//do canvas operations ...
oCanvas.CacheMode = oTempCM;

Thank you @Clemens.



来源:https://stackoverflow.com/questions/32456183/canvas-with-bitmapcache-render-after-removing-child

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!