Child Window in WPF

Let us assume there is a requirement,

1. On click event of button, a new child window should open.

2. While child window is open, the parent window should be inactive.

So start with

Step 1

Create a WPF application. And drag and drop a Button on the MainPage.


Step 2

Right click on the WPF project and new item and select a WPF Window from WPF tab. Rename window to ChildWindow.xaml


Step 3

Now on the click event of button child window will get open.


On the button click event

1. An instance of Child window is being created

2. Then ShowDialog() method is being called to open the child window .


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication2
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
        public MainWindow()
            BtnNewWindow.Click += new RoutedEventHandler(delegate(object sender, RoutedEventArgs e)
                                                              ChildWindow chldWindow = new ChildWindow();



So on running on the click of button new child window being open.


Data Binding In WPF


Here in this tutorial we are going to bind data from a collection (LIST) to XAML. We are going to display
Image, Name, Age and Email id of a Person.

Step 1

Open Visual Studio 2008; create a New Project as WPF application. Give this name as Display.
Step 2

Add a business logic class. Right click in solution explorer then add a new class called Person

namespace Display{public partial class Window1 : Window{
public Window1(){
List<Person> persons = new
new Person{Name=“Anuska Sharama”,Age=21,Email=“”,Image=“anuska.jpg”},
new Person {Name=“Asin”,Age=26,Email=“”,Image=“asin.jpg”},
new Person{Name=“Deepika”,Age=25,Email=“”,Image=“deepika.jpg”}
} } }}

 Step 3
Now it is time to write XAML code and bind collection persons to List of XAML.Create a Listbox in XAML as follows

<ListBox x:Name=”list1″>

Name it as list1. Then bind this list1 to collection persons. So again go to


file and add this line of code after creating collection persons

list1.ItemsSource = persons;
list1.ItemsSource = persons;

Step 4

Now add a Item Template and Data template inside listbox in xaml file. Bind images to List box . Now again add Stack panel with orientation Vertical. Then add three labels to display Name, age and email.

Complete Xaml code will look like

<Window x:Class=”Display.Window1″


Title=”Window1″ Height=”300″ Width=”300″>

 <ListBox x:Name=”list1″>


<StackPanel Orientation=”Horizontal”>

<Image Source=”{Binding Image}” Height=”100″ Stretch=”UniformToFill” />
<StackPanel Orientation=”Vertical”>
<Label FontFamily=”Tahoma” FontSize=”20″ Content=”{Binding Name}” />
<Label FontFamily=”Tahoma” FontSize=”20″ Content=”{Binding Age}” />
<Label FontFamily=”Tahoma” FontSize=”20″ Content=”{Binding Email}” />
Run the code to get the desired output.
Happy Coding