Add Method

VisualCollection.Add Method

Appends a Visual to the end of the VisualCollection.

Namespace:  System.Windows.Media
Assembly:  PresentationCore (in PresentationCore.dll)

public int Add(
	Visual visual


Type: System.Windows.Media.Visual
The Visual to append to the VisualCollection.

Return Value

Type: System.Int32
The index in the collection at which visual was added.


An ArgumentException is thrown if the Visual is a root element.

Adding a Visual whose value is null is permitted and does not raise an exception.

The Add method also sets up the parent-child relationship between the parent visual, which is the owner of the VisualCollection, and the child visual.

If you need greater low-level control over the underlying storage implementation of visual child objects of a custom class, consider using the AddVisualChild and RemoveVisualChild methods. When you use these methods, you must define your own storage implementation, and do not use VisualCollection.

The following example shows how to create a VisualCollection and add members to it.

// Create a host visual derived from the FrameworkElement class.
// This class provides layout, event handling, and container support for
// the child visual objects.
public class MyVisualHost : FrameworkElement
    // Create a collection of child visual objects.
    private VisualCollection _children;

    public MyVisualHost()
        _children = new VisualCollection(this);

        // Add the event handler for MouseLeftButtonUp.
        this.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(MyVisualHost_MouseLeftButtonUp);


For the complete sample, see Hit Test Using DrawingVisuals Sample.

.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

© 2016 Microsoft