WPF Add control Panel in VB.NET

In this article you will learn that how you can add control in a Panel.
  • 3466
Download Files:
 

Introduction
 
Here in this article I am explaining that how you can add controls to a panel. As we have used StackPanel. StackPanel arranges the content either vertically or horizontally. The by default setting of the StcakPanel is Vertical. StackPanel generally Stacks the element. Firstly we will add a StackPanel. In StackPanel we will add a TextBlock and a Tab control. When you clicked the Tab control, a Button control should be added to the StackPanel. The Implementation  needs a StackPanel, TextBlock and Tab control.

Getting Started

  • Simply create a new WPF application.
  • Drag a StackPanel, TextBlock and a Tab control on MainWindow. Your window will look like below.

    addcontrol1.gif
     
  • your MainWindow.xaml will look like below.

    <Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="290" Width
    ="287">
        <StackPanel>
            <TextBlock Name="txt" FontSize="16">How to add control to a Panel</TextBlock>
            <TabControl>
                <TabItem MouseLeftButtonUp="AddButton">
                    <TabItem.Header>Click for Add Control</TabItem.Header>
                </TabItem>
            </TabControl>
            <StackPanel Name="stackpan1"></StackPanel>
        </StackPanel>
    </
    Window>
     
  • Then attach the below code in code behind file.

        
    Private addbtn As System.Windows.Controls.Button, addbtn1 AsSystem.Windows.Controls.Button
         addbtn2 As System.Windows.Controls.Button,addbtn3 AsSystem.Windows.Controls.Button
         Private Sub btnAdd(sender As Object, e As MouseButtonEventArgs)
            stackpan1.Children.Clear()
            addbtn = New Button()
            addbtn.Content = "i have added a New Button"
            stackpan1.Children.Add(addbtn)
        End Sub
     
  • Now run your application.
Output:-

addcontrol2.gif

addcontrol3.gif

Summary 

In this article you learned that how you can add control in a Panel in WPF.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.