Windows Presentation Foundation

last modified on 6/3/2015

Valid XHTML 1.0 Transitional


Contact Info: pmp@pmconsult.com

Disclaimer: Please contact me via email if you think that I've made some sort of mistake on this web site (or in my class). I'll either correct the info or rethink the presentation in an attempt to eliminate confusion.

Class powerpoint and samples: WPF_class_10weekNew.zip


Syllabus for Winter Quarter 2010: WPF_winter_2010.pdf

Meeting 01

Reading (note that some of the samples for these articles is pre-release and will not build. If you wish to see corrected sample for these, then please ask me to provide them...

  1. Development Breakthroughs In Windows Presentation Foundation
  2. THE MODEL-VIEW-VIEWMODEL (MVVM) DESIGN PATTERN FOR WPF
  3. Advanced WPF: Understanding Routed Events And Commands In WPF (we will soon be looking at Routed Events and Commands)

Samples : Meeting01.zip

Questions:

  1. I was asked about some 'missing' controls in WPF that are available in WinForms (like the CheckListBox). I said that it is trival to create one in WPF....but it is trivial only if you are familar with the binding and templates required to create one. Note that we have not dicussed these topics as yet...but here is a sample of a standard listbox, check listbox and radiobutton listbox: ListBoxes.zip

Meeting 02

Samples: Meeting02.zip

Questions from class:

  1. I was wrong about the .Net 4.0 GridSplitter....it is just a buggy as prior versions. I've added a sample to the Meeting02.zip that will demo this issue (and provide some hints as to what is wrong and how to reproduce the bug).
  2. I was asked about window forms and WPF interop just before class started. If time allows, I'll cover a bit of interop later in the course. In the mean time, here is a demo solution that contains 4 projects: a WinForm user control, a WPF user control, a WinForm application that hosts the WPF user control, and a WPF application that hosts the WinForm user control.: WPFandFormsIntegrationDemo.zip

Meeting 03

Samples: Meeting03.zip

Questions from class:

  1. I was asked how to create a data template in code. Note that I'm not planning to discuss this technique in the class (but since I was asked, I'm posting a sample). So I took the lab 1 solution and then commented out the App.XAML resources for DataTemplate and added the code for doing the same in the App.XAML.cs code behind file: PatientReferralsMod1.zip
  2. Also, I was asked another question about the DataTemplate. This question was about the use of the StackPanel with two TextBlocks. I suggested that there was a way to reduce the number of Visual elements (with the cost being an addtion bit of code) and something called MultiBinding. The topic of MultiBinding is to be discussed in a future lesson (but we will not dwell on it very long). Here is another modified version of the Lab 1 solution where I use a single TextBlock for the Patient and a single TextBlock for the Specialist (no StackPanel). Note that the amount of XAML did not get reduced (it actually increased in complexity)...also I now require an implemenation of IMultiValueConverter (see the new source file called PatientSpecialistConverter.cs). I also said that these few Visual elements in these data templates are nothing comparied to what standard controls do. Well, here is the sample (see App.xaml and PaitenSpecialistConverter.cs): PatientReferralsMod1_MultiBinding.zip
  3. I was asked about XAML and if it was faster to to load an application when using XAML versus code-only. In WPF, code only loads faster....but the price is a bit too steep for a marginal performance gain. The reason I'm adding this note is for anyone using Silverlight. First of all, Silverlight is, at its core, an unmanaged platform (with managed wrappers). In Silverlight, XAML tends to be faster than code...Also, in Silverlight, you can only do many tasks only in XAML...quite the opposite from WPF!
  4. Last night I got an email from a student about the lab 01:
    'I was on point 8 of Task1 in Exercise 3 for creation of decription page where I have to create a Button. In "Command" it asks to set value={x:Static NavigationCommands.NextPage}. Question is why does it ask to do it like , why cant I just say Command="NextPage"'
    My answer was as follows: The author of the lab apparently wished to show you that you have multiple choices when writing your XAML. Check out page 272 of the book Pro WPF in C# 2010.
    At the top of the page, a code snippet show how you can use either
    Command = "ApplicationCommands.New"  
    or
    Command = "New"
    This short cut only works for the built-in commands (ApplicationCommands, NavigationCommands, EditingCommands, ComponentCommands, and MediaCommands). If you create your own custom commands, then you must fully qualify the names when you assign them to the Command parameter.
    NOTE: you might find the following syntax in source code:
    Command = "{x:Static ApplicaitonCommands.New}"
    this will work, but takes more typing...as it turns out, there exists a TypeConverter for string to RoutedUICommand that allows the shorter syntax:
    Command="ApplicationCommands.New" or Command="New"
    and this same converter can be used for your custom commands (you just need the full type name)

Meeting 04

Samples: Meeting04.zip

Questions from class:

  1. I said that there was an MSDN article written to address writing XAML that can be shared between Silverlight and WPF. I dont' think that this is a good use of your efforts, but in case you might be interested here it is: Cutting Edge: Code reuse in WPF and Silverlight 2.
  2. At the end of class, I was asked about the difference between RoutedCommand and RoutedUICommand (and why the lab had used RoutedCommand versus RoutedUICommand). The only difference between these two classes (other than that RoutedCommand is the base class of RoutedUICommand) is that RoutedUICommand defines an extra property Text. This Text property can be used directly within MenuItem (or with some work with a fancy binding expression) Buttons. Here is an example: RoutedUICommandDemo.zip
    Note that you can use RoutedUICommand to provide localizable text for a command, but you can't specify mnonics...so if you wish to provide keyboard shortcuts, then you are probably not going to use the Text provided by RoutedUICommand.
  3. I had some question about focus management right before class. The example was this: "given that I have two group boxes, each with a collection of nested controls...how can I enable a behavior where the tab key will cycle through the first group box controls? If so, then how can the user (using the keyboard only) navigate to the 2nd group box controls? ...and how can I force focus to a given control when the window is created?"
    Ok, first off you need to look at the FocusManager and KeyboardNavigation classes. Also note that each control has a TabIndex property that, by default, is set to maximum integer value.
    Here is my starting application with two group boxes with nested controls and default tab behavior:FocusDemo.zip
    Now here is my modified version using KeyboardNavigation to alter the behavior of the first group box (click the Tab key a few times to see). Also, note that I've set the 2nd control in the 2nd group box to have initial focus by using the FocusManager (see the attribute in the Window top-level element). Note that I could have accomplished the same thing just by setting the TabIndex of the control to zero...but doing so would have changed the tab behavior of the surrounding controls. Note that to navigate to the other group box controls using the keyboard can be accomplished by pressing Control-Tab: FocusDemo2.zip

Meeting 05

Samples: Meeting05.zip

Correction to the lab:

  1. There is an error in the lab instructions for lab 3, exercise 4, Task 3, step 3:
    error lab 3 snippet
    The problem is the line TargetType="x:Type Button" where the curley braces are missing. The corrected line should be TargetType="{x:Type Button}"

Meeting 06

Samples: Meeting06.zip

Corrections to lab (6460 module 4 lab):

  1. the starting point for this lab does not compile. I am a big fan of always-building while modifying...so here are my suggestions to get this lab building right away
    1. add the following using statements to the MainWindow.xaml.cs file:
      using WinForms = System.Windows.Forms;
      using System.Globalization;
      using Contoso.Windows.Integration;
      using Contoso.Data;
    2. add the following stubbed-out property to MainWindow.xaml.cs:
      public Appointment Appointment { get { return null; } set {}}
      you will be asked to provide the implementation of this early on (Exercise 1, Task 3, Step 5)
  2. Do the lab in this order (this will allow you to keep the lab building and runnable):
    1. Ex 1
    2. Ex 3
    3. Ex 2
    4. Ex 4

Meeting 07

Samples: Meeting07.zip

Questions from class:

  1. I was asked about Menu's and if you can re-orient them (sideways). There is no built in options for doing so (like the Orientation property of the StackPanel), but you can make use of LayoutTransform to rotate the menu if you wish. Here is a sample: SidewaysMenu.zip
  2. I completed the implemenation of my MVVM sample. The update is in the refreshed zip file for the meeting 7 samples (see like above)
  3. I was asked about multiple view models and how to deal with these via DataContext/Binding. Here is my additional version of the shopping list (based on the step 4 sample of the MVVM sample from the class demo:ShoppingListApp5.zip
  4.  Binding validation configuration options TBD

Corrections to lab (6460 module 5 lab)

  1. Excersize 1, Task 6, step 8 has an error for the C# snippet (the VB snippet is good). It reads:
    error in lab5
    The problem is the element name is not filter but should be filterBox

Meeting 08

Samples:Meeting08.zip

Meeting 09

Samples:Meeting09.zip

Improved PathMarkupDemo2 application (for showing the Path markup from the labs: PathMarkupDemo2.zip

Update to the UIThreading demo in the meeting 9 sample supplied to you last week. This implements a background thread and shows a way to use Sync Context to transition to a caller thread: UIThreadDemos.zip

Meeting 10

New class sample: NumericEditBox.zip

Steps to create a Setup project for custom Performance Monitor counters (from the last class):

  1. Start Visual Studio, create a new DLL project (class library). I'll name my new project "CustomPerfCountersDemo"
  2. Delete the code file provided by the project template (you will not need it)
  3. Add a new Item: Installer Class
  4. You now should see the Installer Class designer. You now should drag the PerformanceCounterInstaller onto the design surface from the toolbar...except, by default, the PerformanceCounterInstaller is not in the toolbar...so you need to add it (see next step)
  5. Open the toolbar, select the General tab and right click to select the Choose Items... menu item. Wait for a bit for the dialog to appear, then seach for and select the PerformanceCounterInstaller. Make sure this item is checked and press OK.
  6. Now double click or drag the PerformanceCounterInstaller to the Installer class design surface.
  7. Make sure you select the PerformanceCounterInstaller on the Installer design surface and open the property page (if it is not already open).
  8. In the Property Page, type in a CategoryName (any name will do, but remember this name...you will need to reference it when you start coding against the counters later...and when you select the counters in PerfMon.exe). I'm calling mine PetersCustomCounters
  9. Now select the Counters property item and press the ellipsis button. You should now see a PerformanceCounter collection editior.
  10. Press the Add button. Name the new counter something (I'll name mine FrameRate), and choose a counter type (the easiest type of counter to manage is a RateOfCountsPerSecond32 or 64)...I'll choose RateOfCountsPerSecond32
  11. Press the Add button again to add another couter...or press OK to dismiss the dialog (I'll add another counter, this time a simple raw counter type of name "ObjectCount" of type NumberOfItems32)
  12. all done...except you need to install this custom counter dll. So lets now add a new project type....
  13. Right mouse click the solution and 'add new project' of type 'Other Project Types' and choose 'Setup and Deployment' ...a  'Setup Project'
  14. You should be shown a File System editor, right click the Application folder and select Add...Project Output. Select the Project "CustomPerfCountersDemo" (the project that was the DLL where we defined our custom counters). Select 'Primary Output" from the list and press OK
  15. Now make sure that the Setup project is selected in your Solution explorer. You should be able to see a toolbar within the solution explorer and select the 2nd to last icon (this should be the Custom Actions editor.
  16. Within the Custom action editor, select the top level node named "Custom Actions" and right mouse click. Select Custom Actions... item from the menu. In the dialog, double click the Application Folder and select the Primary Output. Press the OK button. You should now see that all of the custom actions (Install, Commit, Rollback, and Unintall) should not reference your primary output.
  17. Build the solution, then be sure to select the Setup project in your solution explorer and right mouse click. Select Build or Rebuild.
  18. After you build the setup, you can now install it (you can right mouse click the setup project in solution explorer and select Install)
  19. Now you can make use of the custom counters in a real project file. I'll add a new project (a WPF applicaton) called 'TestBed" to my solution.
  20. To make use of the counters, create PerformanceCounter instances in your app (use the category name and counter name) and for raw counters call the Increment and Decrement methods; for rate counters just call the Increment (and the system will calc the rate of call and decrement it for you). Here is my sample: CustomPerfCountersDemo.zip

 



Pro WPF in C# 2010 by Matthew MacDonald, APress

My List of Issues with this book: Book Comments and Corrections

(Here is a link to my comments on MacDonalds' 2008 book just in case you are interested: Pro WPF 2008 book comments)

Link to my instructor class notes: WPF Class Instructor Notes

Meeting Topics Module
(ppt slide)
Reading Lab (6460 course labs) Additional details
1 introduction 1 chapters 1,2    
2 layout and panels 2 chapter 3    
3 controls, dependency properties, routed events 3,4 chapters 4,5,6 module 1 lab ex 1,2,3 skip the end of chapter 5: MultiTouch. pp 149-157
4  application, windows (dialogs, navigation windows, settings), commands, resources and styles 5,6,7 chapters 7,9,10,11, 23,24 module 2 lab: ex 1,2, 3 (or modified 3 for .Net 4) ch 23, pp 756-758 describes application settings
ch 7 skip localization (pp 239-248)
ch 11 skip behaviors (pp 325-331)
ch 23 skip win7 specific sections (pp 774-789)
ch 24 skip XBAP (pp 819-840)
5 2D graphics, framework templates (control templates and data templates) 8,9 chapters 12,13,14, 17, 20 module 3 lab ex 1,2,3 ch 20 pp 668-672 covers DataTemplate 
6 data binding,  notification subsystem (INotifyPropertyChanged, ObservableCollection<T>, freezable types, etc) 10,11 chapters 8 ,19, 20 module 4 lab: ex 1,2,3,4  
7 views, lists, trees, toolbars, menus; Focus management 12,13,14 chapters 20, 21, 22, 25 module 5 lab: ex 1,2  
8 Documents, Printing, Animation, DependencyProperties revisited 15,16 chapters 28,29, 15, 16 module 3 lab ex 4, module 7 lab: ex 1,2 (skip 3)  
9 Threading
multi-media (sound/video); 3D intro and simple/subtle 3D effects
17
18, 19
chapter 31
chapters 26, 27
module 8 lab: ex 1,2,3,4  
10 custom controls ; Command revisited
depolyment
20,21 chapters 18 module 6 lab: ex 1
module 9 lab: ex 1,2 (skip 3)
 

List of possible additional topics: