问题
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