Click here to Skip to main content
15,889,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Following an example from 'Pro WPF in C# 2008' by Matthew Macdonald, I have implemented a custom version of a Canvas (relevant code shown below).

The AddVisual() and RemoveVisual() methods have no effect on the values I print out from the base Canvas class, and so I am wondering what collections are modified by Canvas.AddVisualChild(), Canvas.AddLogicalChild(), Canvas.RemoveVisualChild() and Canvas.RemoveLogicalChild().

The main point of why I am asking is that I want to be able to 'reset' my custom canvas - i.e. I want to remove all Visual instances from it and start from scratch. I can call _visuals.Clear() to clear my custom list but what do I need to do in the base class?

Any help would be appreciated.

C#
public class MyCanvas : Canvas
{
	public void AddVisual( Visual visual )
	{
		_visuals.Add( visual );
		base.AddVisualChild( visual );
		base.AddLogicalChild( visual );
		Console.WriteLine( $"_visuals.Count={_visuals.Count}" );
		Console.WriteLine( $"base.Children.Count={base.Children.Count}" );
		Console.WriteLine( $"base.VisualChildrenCount={base.VisualChildrenCount}" );
	}

	public void RemoveVisual( Visual visual )
	{
		_visuals.Remove( visual );
		base.RemoveVisualChild( visual );
		base.RemoveLogicalChild( visual );
		Console.WriteLine( $"_visuals.Count={_visuals.Count}" );
		Console.WriteLine( $"base.Children.Count={base.Children.Count}" );
		Console.WriteLine( $"base.VisualChildrenCount={base.VisualChildrenCount}" );
	}

	public void ClearAll()
	{
		_visuals.Clear();
		// WHAT GOES HERE?
	}
		
	protected override int VisualChildrenCount { get { return _visuals.Count; } }

	protected override Visual GetVisualChild( int index ) { return _visuals[ index ]; }

	private List<Visual> _visuals = new List<Visual>();
}


What I have tried:

I have tried searching articles on AddLogicalChild() and AddVisualChild(), but nowhere can I get an explanation of what collections these functions manipulate and how to 'reset' them.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900