VisualCollection.Add(Visual) Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Appends a Visual to the end of the VisualCollection.
public:
int Add(System::Windows::Media::Visual ^ visual);
public int Add(System.Windows.Media.Visual visual);
member this.Add : System.Windows.Media.Visual -> int
Public Function Add (visual As Visual) As Integer
- visual
- Visual
The Visual to append to the VisualCollection.
The index in the collection at which visual
was added.
An ArgumentException is thrown if the Visual is a root element.
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);
_children.Add(CreateDrawingVisualRectangle());
_children.Add(CreateDrawingVisualText());
_children.Add(CreateDrawingVisualEllipses());
// Add the event handler for MouseLeftButtonUp.
this.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(MyVisualHost_MouseLeftButtonUp);
}
' 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
Inherits FrameworkElement
' Create a collection of child visual objects.
Private _children As VisualCollection
Public Sub New()
_children = New VisualCollection(Me)
_children.Add(CreateDrawingVisualRectangle())
_children.Add(CreateDrawingVisualText())
_children.Add(CreateDrawingVisualEllipses())
' Add the event handler for MouseLeftButtonUp.
AddHandler MouseLeftButtonUp, AddressOf MyVisualHost_MouseLeftButtonUp
End Sub
Note
For the complete sample, see Hit Test Using DrawingVisuals Sample.
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.
Product | Versions |
---|---|
.NET Framework | 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
Windows Desktop | 3.0, 3.1, 5, 6, 7, 8, 9, 10 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: