Free Exam Guide 70--511: XAML, DataTemplates, Window.Resources and comix galore

 

image

CRO is just not use to working with XAML, there are three things you need to keep in mind when working with XAML:

  • 1. It is just XML that has specific tags for use in presenting data
  • 2. XAML works with all of the classes in a specified namespace
  • 3.  Binding can be tricky
  • 4. To be comfortable with the use of XAML, you do need to memorize a bunch of stuff

In the case we are reviewing today, we need to see specifically how to bind information to the code.

First before we start on the XAML Code, let’s enlarge the XAML Text font size, so select the Tools Options and then Font and Colors.  Finally select XAML Text and then Size.

image

Now back to the XAML (

The code is in an image, the three colored boxes state:

  • (Blue box) Partial Class definition
  • (Yellow box) Namespace that has the Classes for use by the XAML
  • (Green box) x:Key uniquely identifies elements that are created and referenced in a XAML-defined dictionary (from link:

 

image

 

In the XAML code, first of all, if you experiment with the xmlns:local, and change this to xmln:pineapple, also change the <local:People x:Key=”MyFriends”/> to <pineapple:People x:Key=”MyFriends”/>

If you do everything correctly you will see that the variable local is just that, a variable.  Local might seem like a keyword, but it isn’t, and that can be confusing when you are troubleshooting.

Now why to use the DataTemplate? (From: https://bit.ly/datatemplate, checked April 5, 2011)

  • You use a DataTemplate to specify the visualization of your data objects.
  • DataTemplate objects are particularly useful when you are binding an ItemsControl such as a ListBox to an entire collection.
  • Without specific instructions, a ListBox displays the string representation of the objects in a collection.
  • In that case, you can use a DataTemplate to define the appearance of your data objects.
  • The content of your DataTemplate becomes the visual structure of your data objects.

In our very simple example, there is only the first names with no label, but in the actual SDKSample, there are more items like last name and city.

The DataTemplate allows you to organize controls, for instance control that are based on the ItemsControl, such as ListBox.  The content of the DataTemplate becomes the visual structure of your data objects.  However, since Armando, CRO, and that new person, selected the DataTemplate, can they do a hierarchical presentation like the Boss Rebecca ask for?

The <Window.Resources></Window.Resources> in a FrameworkElement.Resources, and this will get or set the locally-defined resource dictionary.  Really.  What does this mean.  Window.Resoures, Page.Resources, are ways to organize your resources.  Note that Armando and the new girl put the “s” in the wrong place, it is Window.Resources or Page.Resources or StackPanel.Resrouces, etc.  CRO got it right.

image

In the code example, the Window.Resources contains the lines:

<Window.Resources>
    <local:People x:Key="MyFriends"/>

<DataTemplate x:Key="DetailTemplate">

The lines after  <DataTemplate x:Key="DetailTemplate"> detail or sets the styles for use in the rest of the XAML code (keep in mind that the XAML code is rather short compared to production XAML code). 

Further down the code has the lines:

<ContentControl Content="{Binding Source={StaticResource MyFriends}}"
                    ContentTemplate="{StaticResource DetailTemplate}"/>

The DataTemplate is consumed as a StaticResource and uses the DataTemplate with the x:Key=”DetailTemplate”

The ContentControl represents a control with a single piece of content of any type.

A StaticResource is generated at the initial runtime and doesn’t change.

image