VisualCollection.Add(Visual) メソッド

定義

VisualCollectionの末尾にVisualを追加します。

public:
 int Add(System::Windows::Media::Visual ^ visual);
public int Add(System.Windows.Media.Visual visual);
member this.Add : System.Windows.Media.Visual -> int
Public Function Add (visual As Visual) As Integer

パラメーター

visual
Visual

VisualCollectionに追加するVisual

返品

visualが追加されたコレクション内のインデックス。

例外

Visualがルート要素の場合、ArgumentExceptionがスローされます。

次の例では、 VisualCollection を作成し、それにメンバーを追加する方法を示します。

// Create a host visual derived from the FrameworkElement class.
// This class provides layout, event handling, and container support for
// the child visual objects.
public class MyVisualHost : FrameworkElement
{
    // Create a collection of child visual objects.
    private VisualCollection _children;

    public MyVisualHost()
    {
        _children = new VisualCollection(this);
        _children.Add(CreateDrawingVisualRectangle());
        _children.Add(CreateDrawingVisualText());
        _children.Add(CreateDrawingVisualEllipses());

        // Add the event handler for MouseLeftButtonUp.
        this.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(MyVisualHost_MouseLeftButtonUp);
    }
' Create a host visual derived from the FrameworkElement class.
' This class provides layout, event handling, and container support for
' the child visual objects.
Public Class MyVisualHost
    Inherits FrameworkElement
    ' Create a collection of child visual objects.
    Private _children As VisualCollection

    Public Sub New()
        _children = New VisualCollection(Me)
        _children.Add(CreateDrawingVisualRectangle())
        _children.Add(CreateDrawingVisualText())
        _children.Add(CreateDrawingVisualEllipses())

        ' Add the event handler for MouseLeftButtonUp.
        AddHandler MouseLeftButtonUp, AddressOf MyVisualHost_MouseLeftButtonUp
    End Sub

Note

完全なサンプルについては、「 DrawingVisuals サンプルを使用したヒット テスト」を参照してください。

注釈

null値を持つVisualを追加することは許可されており、例外は発生しません。

Add メソッドでは、VisualCollectionの所有者である親ビジュアルと子visualの間に親子関係も設定します。

カスタム クラスのビジュアル子オブジェクトの基になるストレージ実装をより低レベルで制御する必要がある場合は、 AddVisualChild メソッドと RemoveVisualChild メソッドの使用を検討してください。 これらのメソッドを使用する場合は、独自のストレージ実装を定義する必要があり、 VisualCollectionは使用しないでください。

適用対象