Visual.RemoveVisualChild(Visual) Visual.RemoveVisualChild(Visual) Visual.RemoveVisualChild(Visual) Visual.RemoveVisualChild(Visual) Method

定義

移除兩個視覺物件之間的父-子關係。Removes the parent-child relationship between two visuals.

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

參數

child
Visual Visual Visual Visual

要從父視覺物件移除的子視覺物件。The child visual object to remove from the parent visual.

範例

下列範例示範如何定義自訂的儲存體需求視覺子系。The following example shows how to define custom storage requirements for a visual child. 此範例會使用AddVisualChildRemoveVisualChild將視覺化父代之間的父子式關聯性的方法和childThe 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

備註

RemoveVisualChild方法會移除兩個視覺效果之間的父子式關聯性。The RemoveVisualChild method removes the parent-child relationship between two visuals. 這種方法,連同AddVisualChild方法,必須使用當您需要更有效地視覺子物件的基礎儲存體實作的低階控制。This method, along with the AddVisualChild 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.

適用於

另請參閱