Visual.RemoveVisualChild(Visual) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
移除两个可视对象之间的父子关系。
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
要从父可视对象中移除的子可视对象。
示例
以下示例演示如何为视觉对象子级定义自定义存储要求。 本示例使用 AddVisualChild 和 RemoveVisualChild 方法设置父视觉对象与 child
父视觉对象之间的父子关系。 为了使可视化树能够正确枚举,此示例提供重写的方法和VisualChildrenCount属性的实现GetVisualChild。
备注
尽管可用于 VisualCollection 在视觉对象之间创建父子关系,但在仅将一个子级链接到父级时,提供自己的自定义存储实现更为高效。
// 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 删除两个视觉对象之间的父子关系。 当需要对视觉子对象的基础存储实现进行更大的低级别控制时,必须使用此方法以及 AddVisualChild 该方法。 VisualCollection 可用作存储子对象的默认实现。