FrameworkElement.GetVisualChild(Int32) FrameworkElement.GetVisualChild(Int32) FrameworkElement.GetVisualChild(Int32) FrameworkElement.GetVisualChild(Int32) Method

定义

重写 GetVisualChild(Int32),并从子元素集合中返回指定索引处的子元素。Overrides GetVisualChild(Int32), and returns a child at the specified index from a collection of child elements.

protected:
 override System::Windows::Media::Visual ^ GetVisualChild(int index);
protected override System.Windows.Media.Visual GetVisualChild (int index);
override this.GetVisualChild : int -> System.Windows.Media.Visual
Protected Overrides Function GetVisualChild (index As Integer) As Visual

参数

index
Int32 Int32 Int32 Int32

集合中请求的子元素的从零开始的索引。The zero-based index of the requested child element in the collection.

返回

请求的子元素。The requested child element. 这不应返回 null;如果提供的索引超出范围,则引发异常。This should not return null; if the provided index is out of range, an exception is thrown.

示例

下面的示例演示自定义装饰器如何使用其为多个VisualCollection视觉对象维护的的值。The following example shows how a custom adorner uses the values declared by a VisualCollection that it maintains for its multiple visual children. 这些值通过和VisualChildrenCount GetVisualChild的重写进行报告。These values are reported through overrides of VisualChildrenCount and GetVisualChild.

// To store and manage the adorner's visual children.
VisualCollection visualChildren;
' To store and manage the adorner's visual children.
Private visualChildren As VisualCollection
// Override the VisualChildrenCount and GetVisualChild properties to interface with 
// the adorner's visual collection.
protected override int VisualChildrenCount { get { return visualChildren.Count; } }
protected override Visual GetVisualChild(int index) { return visualChildren[index]; }
' Override the VisualChildrenCount and GetVisualChild properties to interface with 
' the adorner's visual collection.
Protected Overrides ReadOnly Property VisualChildrenCount() As Integer
    Get
        Return visualChildren.Count
    End Get
End Property
Protected Overrides Function GetVisualChild(ByVal index As Integer) As Visual
    Return visualChildren(index)
End Function

注解

FrameworkElement在实现中, 唯一有效的索引为零。In the FrameworkElement implementation, the only valid index is zero. 的内容模型GetVisualChild支持零个或一个子元素, 而不是一个集合。The content model for GetVisualChild supports either zero or one child elements, not a collection.

继承者说明

此实现仅对没有维护更具描述性的可视化子元素集合的元素有效。This implementation is only valid for elements that do not maintain any more descriptive collection of visual child elements. 确实具有此集合的任何元素都必须重写此方法, 并将索引映射到该元素支持的子元素集合中的等效索引。Any element that does have such a collection must override this method and map the index to an equivalent index in the child element collection that is supported by that element. 范围从零到VisualChildrenCount (减一) 的索引应返回有效的元素; 任何其他索引应引发超出范围的异常。An index in the range from zero to VisualChildrenCount (minus one) should return a valid element; any other index should throw an out-of-range exception. 支持子集合的元素类型的示例和重写GetVisualChild(Int32)可以返回多个可能的Panel子级。An example of an element type that does support a child collection and overrides GetVisualChild(Int32) to return more than one possible child is Panel.

FrameworkElement的默认实现仅假定一个视觉对象。The default implementation in FrameworkElement presumes only one visual child. 传递给零以外index的任何值都将导致引发异常。Any value passed for index other than zero causes an exception to be thrown. 几个常见元素 (如修饰器、装饰器或具有专用呈现的元素) 将FrameworkElement重写实现 (从中间基类实现)。Several common elements, such as decorators, adorners, or elements with specialized rendering, override the FrameworkElement implementation (of the implementation from intermediate base classes). 某些实现仍强制实施一个视觉对象, 而其他实现则允许集合。Some implementations still enforce one visual child whereas others allow a collection.

适用于