Tuesday, September 07, 2010

Child Window in WPF

Let us assume there is a requirement, On click event of button, a new child window should open. 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.
<Grid>

<Button x:Name="ButtonNewWindow"  Hright="100" Width="Auto" Content="Open new Window" />
</Grid>
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.
ButtonNewWindow.Click += new RoutedEventHandler (delegate(object sender, RoutedEventArgs e)

{
ChildWindow chlWindow = new ChildWindow();
MessageBox.Show(chlWindow.GetMessage());
chlWindow.ShowDialog();
}

On the button click event. An instance of Child window is being created. Then ShowDialog() method is being called to open the child window .
Read more: C# Corner