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

定义

定义两个可视对象之间的父子关系。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)

参数

child
Visual Visual Visual Visual

要添加到父可视对象的子可视对象。The child visual object to add to parent visual.

示例

下面的示例演示如何为视觉对象定义自定义存储要求。The following example shows how to define custom storage requirements for a visual child. 该示例使用AddVisualChildRemoveVisualChild方法来设置父视觉对象和child之间的父子关系。The example uses the AddVisualChild and RemoveVisualChild methods to sets up the parent-child relationship between the parent visual and child. 为了正确枚举可视化树, 该示例提供GetVisualChild方法和VisualChildrenCount属性的重写实现。In order for the visual tree to be enumerated correctly, the example provides overridden implementations of the GetVisualChild method and VisualChildrenCount property.

备注

尽管可以使用VisualCollection创建可视对象之间的父子关系, 但当只有一个子级链接到父对象时, 提供自己的自定义存储实现会更有效。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

注解

AddVisualChild方法设置两个可视对象之间的父子关系。The AddVisualChild method sets up the parent-child relationship between two visual objects. 如果需要对可视子对象的基础存储实现进行更高级别的控制, 则必须使用此方法。This method must be used when you need greater low-level control over the underlying storage implementation of visual child objects. VisualCollection可用作存储子对象的默认实现。VisualCollection can be used as a default implementation for storing child objects.

适用于

另请参阅