Share via


方法 : 要素を動的に追加する

更新 : 2007 年 11 月

次の例は、Children プロパティの Add メソッドを使用して、Button 要素を StackPanel に追加する方法を示しています。

TabControl を作成する Extensible Application Markup Language (XAML) の例を次に示します。ユーザーが [Add Control] タブをクリックすると、コードに記述されている MouseLeftButtonDown イベント ハンドラの AddButton が既存のコントロールをすべて消去し、新しいボタンを追加します。

使用例

<TabControl>
<TabItem MouseLeftButtonUp="AddButton">
<TabItem.Header>Add Control</TabItem.Header>
</TabItem>
       void AddButton(object sender, MouseButtonEventArgs e)
        {
            sp1.Children.Clear();
            btn = new Button();
            btn.Content = "New Button";
            sp1.Children.Add(btn);
        }
Private Sub AddButton(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
    sp1.Children.Clear()
    btn = New Button()
    btn.Content = "New Button"
    sp1.Children.Add(btn)
End Sub
ms752374.alert_note(ja-jp,VS.90).gifメモ :

サンプル全体については、「要素の使用のサンプル」を参照してください。

参照

概念

パネルの概要

参照

UIElementCollection

UIElement