Search code examples
c#wpfwpf-controlsuielement

UIElement was removed from the canvas.Children collection, but it still drawn. Why is this?


I try to remove an UIElement from the Canvas. But although it is removed from the canvas.Children collection, is still remains drawn on the canvas.

Do you know any solution to this problem?


Solution

  • What you say is not true must be true and so you will have to use tools to resolve the apparent contradiction.

    The first and most traditional tool is the debugger. You can literally inspect the Children property of the Canvas and see if the element is there even though you think you removed it. This is the "trust but verify" principle. Normally you don't have to verify common sense things but when you have a contradiction it's time to start verifying.

    The second tool, Snoop, is very powerful and ideally suited for your situation:

    With this tool you can find the rogue object in the visual tree, see its parent, see its properties, etc. This might convince you that it is indeed still part of the visual tree, but perhaps not where you thought it was.