Add Method

Adds an object to the end of the collection.

Cannot use methods in XAML.
retval = object.Add(value)




The object to add to the collection.

Return Value


Returns the zero-based index of the inserted object in the collection if successful; otherwise, returns null.


Certain collections place importance on the collection order. For instance, transforms in a TransformCollection are applied in collection order, and child objects in the VisualCollection for the Canvas.Children property are rendered in collection order (each object potentially rendering on top of previous objects). For these collections, you might want to use Insert to add the new item at a specific index. In other collections, such as GradientStopCollection, behavior is controlled by specific properties on objects in the collection, and the collection order is unimportant.


You can add XAML content to objects that support child objects, such as the Canvas object. The following JavaScript example shows how to create a TextBlock and add it to the root Canvas object by using the Add method on the object's collection of children. Note that the Opacity property is 0, so the fragment will not be initially visible. Using the Opacity property to control an object's visibility avoids having to modify the Silverlight object hierarchy.

var textBlock;
function onLoaded(sender, eventArgs)
    // Retrieve the id of the plug-in.
    var plugin = sender.getHost();
    // Define and create a XAML fragment.
    var xamlFragment = '<TextBlock Canvas.Top="200" Opacity=".5" Text="Click for more info" />';
    textBlock = plugin.content.createFromXaml(xamlFragment);
    // Add the TextBlock to the root Canvas object.
// Toggle the Opacity property for the TextBlock.
function onToggle()
    if (textBlock.opacity)
        textBlock.opacity = 0;
        textBlock.opacity = 1;

Applies To

ColorKeyFrameCollection, DoubleKeyFrameCollection, GradientStopCollection, Inlines, PathFigureCollection, PathSegmentCollection, PointKeyFrameCollection, StrokeCollection, StylusPointCollection, TriggerActionCollection, TriggerCollection, VisualCollection

See Also

Referencing and Modifying Silverlight Objects