Visual.AddVisualChild(Visual) Visual.AddVisualChild(Visual) Visual.AddVisualChild(Visual) Visual.AddVisualChild(Visual) Method

Definición

Define la relación primario-secundario entre dos objetos visuales.Defines the parent-child relationship between two visuals.

protected:
 void AddVisualChild(System::Windows::Media::Visual ^ child);
protected void AddVisualChild (System.Windows.Media.Visual child);
member this.AddVisualChild : System.Windows.Media.Visual -> unit
Protected Sub AddVisualChild (child As Visual)

Parámetros

child
Visual Visual Visual Visual

Objeto visual secundario que se va a agregar al objeto visual primario.The child visual object to add to parent visual.

Ejemplos

El ejemplo siguiente muestra cómo definir los requisitos de almacenamiento personalizado para un elemento secundario visual.The following example shows how to define custom storage requirements for a visual child. El ejemplo se usa el AddVisualChild y RemoveVisualChild métodos a los conjuntos de la relación de elementos primarios y secundarios entre el elemento primario visual y child.The example uses the AddVisualChild and RemoveVisualChild methods to sets up the parent-child relationship between the parent visual and child. En orden para el árbol visual se enumere correctamente, el ejemplo proporciona implementaciones reemplazadas de la GetVisualChild método y VisualChildrenCount propiedad.In order for the visual tree to be enumerated correctly, the example provides overridden implementations of the GetVisualChild method and VisualChildrenCount property.

Nota

Aunque es posible usar VisualCollection para crear relaciones entre objetos visuales de elementos primarios y secundarios, es más eficaz para proporcionar su propia implementación de almacenamiento personalizado cuando solo un elemento secundario que esté vinculado a un elemento primario.Although it is possible to use VisualCollection to create parent-child relationships between visual objects, it is more efficient to provide your own custom storage implementation when only one child is linked to a parent.

// Create a host visual derived from the FrameworkElement class.
// This class provides layout, event handling, and container support for
// the child visual object.
public class MyVisualHost : FrameworkElement
{
    private DrawingVisual _child;

    public MyVisualHost(DrawingVisual drawingVisual)
    {
        _child = drawingVisual;
        this.AddVisualChild(_child);
    }

    public DrawingVisual Child
    {
        get
        {
            return _child;
        }

        set
        {
            if (_child != value)
            {
                this.RemoveVisualChild(_child);
                _child = value;
                this.AddVisualChild(_child);
            }
        }
    }

    // Provide a required override for the VisualChildrenCount property.
    protected override int VisualChildrenCount
    {
        get { return _child == null ? 0 : 1; }
    }

    // Provide a required override for the GetVisualChild method.
    protected override Visual GetVisualChild(int index)
    {
        if (_child == null)
        {
            throw new ArgumentOutOfRangeException();
        }

        return _child;
    }
' Create a host visual derived from the FrameworkElement class.
' This class provides layout, event handling, and container support for
' the child visual object.
Public Class MyVisualHost
	Inherits FrameworkElement
	Private _child As DrawingVisual

	Public Sub New(ByVal drawingVisual As DrawingVisual)
		_child = drawingVisual
		Me.AddVisualChild(_child)
	End Sub

	Public Property Child() As DrawingVisual
		Get
			Return _child
		End Get

		Set(ByVal value As DrawingVisual)
			If _child IsNot value Then
				Me.RemoveVisualChild(_child)
				_child = value
				Me.AddVisualChild(_child)
			End If
		End Set
	End Property

	' Provide a required override for the VisualChildrenCount property.
	Protected Overrides ReadOnly Property VisualChildrenCount() As Integer
		Get
			Return If(_child Is Nothing, 0, 1)
		End Get
	End Property

	' Provide a required override for the GetVisualChild method.
	Protected Overrides Function GetVisualChild(ByVal index As Integer) As Visual
		If _child Is Nothing Then
			Throw New ArgumentOutOfRangeException()
		End If

		Return _child
	End Function

Comentarios

El AddVisualChild método configura la relación primario-secundario entre dos objetos visuales.The AddVisualChild method sets up the parent-child relationship between two visual objects. Este método se debe usar cuando necesite un mayor control de bajo nivel sobre la implementación subyacente de almacenamiento de objetos visuales secundarios.This method must be used when you need greater low-level control over the underlying storage implementation of visual child objects. VisualCollection puede utilizarse como una implementación predeterminada para almacenar los objetos secundarios.VisualCollection can be used as a default implementation for storing child objects.

Se aplica a

Consulte también: