VisualCollection.Add(Visual) メソッド

定義

VisualVisualCollection の末尾に追加します。

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

Visual の末尾に追加する VisualCollection

戻り値

Int32

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

例外

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

次の例は、メンバーを作成してメンバーを 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

注意

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

注釈

値が Visual 許可され、例外が null 発生しないユーザーを追加します。

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

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

適用対象