Book Image

Windows Presentation Foundation Development Cookbook

Book Image

Windows Presentation Foundation Development Cookbook

Overview of this book

Windows Presentation Foundation (WPF) is Microsoft's development tool for building rich Windows client user experiences that incorporate UIs, media, and documents. With the updates in .NET 4.7, Visual Studio 2017, C# 7, and .NET Standard 2.0, WPF has taken giant strides and is now easier than ever for developers to use. If you want to get an in-depth view of WPF mechanics and capabilities, then this book is for you. The book begins by teaching you about the fundamentals of WPF and then quickly shows you the standard controls and the layout options. It teaches you about data bindings and how to utilize resources and the MVVM pattern to maintain a clean and reusable structure in your code. After this, you will explore the animation capabilities of WPF and see how they integrate with other mechanisms. Towards the end of the book, you will learn about WCF services and explore WPF's support for debugging and asynchronous operations. By the end of the book, you will have a deep understanding of WPF and will know how to build resilient applications.
Table of Contents (13 chapters)
2
Using WPF Standard Controls

How to do it...

Follow these steps to create the dialog window and invoke it from the MainWindow to show a message to the user:

  1. Open the Solution Explorer and right-click on the project node.
  2. From the context menu, select Add | Window... to open the Add New Item dialog.
  3. Making sure that the Window (WPF) template is selected, give it the name MessageDialog, and click Add to continue. This will create MessageDialog.xaml and MessageDialog.xaml.cs files in the project.
  4. Open the MessageDialog.xaml file and replace the entire XAML content with the following:
<Window x:Class="CH01.DialogBoxDemo.MessageDialog" 
 xmlns=
"http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" ShowInTaskbar="False" WindowStyle="SingleBorderWindow" Title="Message" Height="150" Width="400" FontSize="14" Topmost="True" ResizeMode="NoResize">
<Grid> <TextBlock TextWrapping="Wrap" Margin="8" Text="Thank you for reading 'Windows Presentation
Foundation Cookbook'. Click 'OK' to continue next."/> <StackPanel Orientation="Horizontal" VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="4"> <Button Content="OK" Width="60" Height="30" Margin="4" IsDefault="True" Click="OnOKClicked"/> <Button Content="Cancel" Width="60" Height="30" Margin="4" IsCancel="True" Click="OnCancelClicked"/> </StackPanel> </Grid> </Window>
  1. Open the MessageDialog.xaml.cs file, and add the following event implementations for the OK button and Cancel button:
private void OnOKClicked(object sender, RoutedEventArgs e) 
{ 
    DialogResult = true; 
} 
 
private void OnCancelClicked(object sender, RoutedEventArgs e) 
{ 
    DialogResult = false; 
} 
  1. Now open the MainWindow.xaml page and replace the Grid with the following XAML content:
<Grid> 
    <ListBox x:Name="result" Height="100" Margin="8" 
             HorizontalAlignment="Stretch"  
             VerticalAlignment="Top" /> 
    <Button Content="Show Message" Width="150" Height="30"  
            VerticalAlignment="Bottom" Margin="8" 
            Click="OnShowMessageButtonClicked"/> 
</Grid> 
  1. Go to the code-behind file, MainWindow.xaml.cs, and add the button event implementation as shared in the following code section:
private void OnShowMessageButtonClicked(object sender, RoutedEventArgs e) 
{ 
    var messageDialog = new MessageDialog(); 
    var dialogResult = messageDialog.ShowDialog(); 
 
    if (dialogResult == true) 
    { 
        result.Items.Add("You clicked 'OK' button."); 
    } 
    else if (dialogResult == false) 
    { 
        result.Items.Add("You clicked 'Cancel' button."); 
    } 
}
  1. Now run the application. The visible window will have a button labeled Show Message. Click on it to invoke the message dialog window that we have created:
  1. Click on the Cancel button, which will add You clicked 'Cancel' button text into the list present in the MainWindow.
  2. Launch the message window again and click on the OK button. This will add You clicked 'OK' button in the list.