Archive for April, 2009

Unkeyed DataTemplate Resolution Issue

Hello Everyone!

So recently i have been playing around with dynamically injecting a ResourceDictionary that contains an Unkeyed DataTemplate into the Resource Lookup Tree before the Generic.xaml that contains the default DataTemplate targetting the same type.  One would think that if a loaded ContentControl whose Content is an instance of an object that is the same type as the DataTemplate that the UI would find the newly resolved DataTemplate and switch the UI.  I have found that this does not work.  You can do the same thing with a Style and it will properly flip the UI to the definition of the newly injected Style, but not for a DataTemplate.

So the question becomes, how can we provide this setup?  What im going to do today is show you how you can setup your ContentControl to support this functionality!

So the first thing we are going to do is go ahead and give our DataTemplate a Key.

<DataTemplate DataType="{x:Type ssVms:SlideShowViewModel}"
              x:Key="SlideShowViewModelDataTemplate">

Next we are going to bind our ContentControls ContentTemplate property as a “Dynamic” resource as such.

<ContentControl x:Name="SlideShowViewModelHost"
                ContentTemplate="{DynamicResource SlideShowViewModelDataTemplate}"/>

Now the DynamicResource is the key here.  Becuase it is explicitly set as a DynamicResource it causes more evaluation of found resources causing it to be invalidated when you inject a new ResourceDictionary.  Now that we have this we can swap out ResourceDictionaries containing new DataTemplates for our objects and have the UI automatically flip to the new visuals!

Hope this helps anyone else running into this same issue.  Have a good day!

No Comments

Attended Sharepoint Saturday

Hello All!

Last saturday I attended the Sharepoing Saturday conference and it was a blast!

First of all, a big thanks to Dan Attis for setting up and organizing the event, he did a great job!

So I have never actually looked into anything Sharepoint before and wanted to go to this event because I thought it would be a great way to start off.  A good portion of the topics were a good bit advanced (at least to someone new to Sharepoint :) ) so i had some difficulty finding the right sessions to go to but I think the ones i did find were very helpful.

The first session i went to was done by Phil Wicklund and was titled – Development: Building the next Generation of SharePoint Web Parts – how to Leverage JQuery and Silverlight!  In this session we learned how we can include the wildly popular JQuery and Silverlight in our Sharepoint Applications.  I have to say that after seeing this session, I can see a good adoption of both of these technologies into Sharepoint and they will end up becoming key to making very cool Sharepoint applications.  Most of the content was around JQuery and Silverlight itself, but the ease of integration with Sharepoint is the kicker.

I decided to skip out on the second session because all of the topics seemed a bit beyond where i was willing to venture for the moment.  So instead i went out to the common area for the speakers to see if i could have a one on one talk with one of them about getting started with Sharepoint.  I had a great conversation with Michael Lotter about things like key terms to understand, what books and articles would be good to start off with and a general discussion of just the base object model used in Sharepoint.

The third session (but my second) was the one i had been waiting for all day.  The session was Development: Getting started with SharePoint Development by Aaron Cutlip.  In this session we got our first basic dive into some of the beginning end user and development concepts in Sharepoint.  This is a session that can be hard at times because their are many different starting points in Sharepoint and each starting point could cover its own entire day of content to really get an introduction.  With the time frame given i was very impressed with the presentation Aaron gave.  I came out of there understanding a whole lot more about Sharepoint and where i needed to start.

The fourth session (but my third) that i attended was Development: Building Custom List Forms in ASP.NET by Doug Ware.  Doug is a local Sharepoint guy here in Atlanta and is always great to watch doing a presentation.  This topic went over the different ways that a Sharepoint List Form can be customized to reach a large range of goals that it can provide.  This was a more advanced topic and i was only half-confused by him.  He did a great job presenting and relaying the information, i just think i might not have been ready to take all of that information in :)

The last session i decided to skip because I think i had enough information for the day and the rest all seemed like topics i might not understand do much.  So instead i went over to the popular Rock Band setup and tried my best at laying down some riffs and doing some singing with the other people that attended.  I had never played Rock Band before so it was alot of fun for me.

Afterwards we had the closing ceremonies and awards.  They pulled random names for receiving a book as an award and it turned out that they had enough books for every single person to get one.  I personally got one on using Silverlight in Sharepoing :) .  I figured it would be a good match for my WPF/Silverlight specialty.

Lastly but always a blast was the Sharepint meetup at Millers Ale House after the conference.  This is where everyone gets together for some food and drinks afterwards and any furthur discussions.  Always fun to go to these.

Well thats about it for Sharepoint Saturday.  Again a big thanks to Dan Attis for setting all of this up.  And also a thanks to all the sponsers that made this happen.

No Comments

WPF DataTemplates

Hello again All!

Today im going to talk a little on how to use a DataTemplate in WPF.  Many people new to WPF generally run across the need to use a ItemsControl, ListBox, or ListView for their repeating item data.  Usually the first thing they find is that they have to set the ItemTemplate property which uses a DataTemplate to provide how each item should be visually represented.  I find that this is generally the extent of knowledge that people new to WPF have on DataTemplate.  This blog is going to go a bit more in depth into what a DataTemplate is and to show other ways we can use them!

So first lets actually define what a DataTemplate is.  A DataTemplate is a visual template that is a representation of data!  Wow were really into some deep logic here :) .  A DataTemplate directly relates to the concept of Content when using any form of a ContentControl.  To see more information of the WPF ContentControl Content model please see Controls Content Model Overview. When you apply a DataTemplate you are applying the Template to the Content directly.  When we think about what we can do with a DataTemplate we find there are different ways in which we can specify what data we want to template.

First lets go over an ItemsControl example.

When we look at the ItemsControl we have a property called ItemTemplate.  This property receives a DataTemplate.  Lets say we have a list of Person objects and we want to display LastName, FirstName.  This is how we could represent that in code.

<ItemsControl ItemsSource="{StaticResource MyPersonsList}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding LastName}"/>
                <TextBlock Text=", "/>
                <TextBlock Text="{Binding FirstName}"/>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Notice that our DataTemplate contains a horizontal StackPanel and the TextBlocks are arranged in a way that will show something like Doe, John for that entry in the Person list.  We have also not specified any attributes in the DataTemplate.  The reason this works is because control inheriting from ItemsControl automatically knows to go ahead and force the association of the template with the Content of each item in the ItemsControl.  Each item that is generated hosts a Content property which the template is applied to as stated above in defining a DataTemplate.

Before we move on to more complex concepts I’ll show how we can also use this concept in just a standard ContentControl.

<ContentControl Content="{StaticResource MySinglePerson}">
    <ContentControl.ContentTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding LastName}"/>
                <TextBlock Text=", "/>
                <TextBlock Text="{Binding FirstName}"/>
            </StackPanel>
        </DataTemplate>
    </ContentControl.ContentTemplate>
</ContentControl>

As you can see here we have specified the ContentTemplate property to display content for this control in the same way that we did in the ItemsControl.  This again shows the relation to the Content model in WPF where we saying that we want whatever content is set to the control to be displayed in a certain way.

Now that we have seen how to use a DataTemplate we have noticed that we have used the same DataTemplate for two different controls.  Well we definatly do not want to duplicate this everywhere in the application that has the Person object formatted in the same way.  Luckilly for us we can put DataTemplates as keyed resources!

Lets say that we have many different screens that all need to make use of this same data format.  What we can do is go into our App.xaml and add this template with a key to its resource collection.  (Note*  When WPF looks for a resource the App.xaml is the second to last place it checks when finding the resource.  This ensures that all of our screens in this project will be able to find the DataTemplate correctly).  Our App.xaml may look something like this.

<Application x:Class="DataTemplateExample.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Window1.xaml">
    <Application.Resources>
        <DataTemplate x:Key="PersonNameDataTemplate">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding LastName}"/>
                <TextBlock Text=", "/>
                <TextBlock Text="{Binding FirstName}"/>
            </StackPanel>
        </DataTemplate>
    </Application.Resources>
</Application>

What we have done is said that we want to expose a DataTemplate to the entire application called PersonNameDataTemplate to use as we wish.  Now if we modify our ItemsControl and ContentControl code to use this template it would now look like this.

<ItemsControl ItemsSource="{StaticResource MyPersonList}"
              ItemTemplate="{StaticResource PersonNameDataTemplate}"/>

<ContentControl Content="{StaticResource MySinglePerson}"
                ContentTemplate="{StaticResource PersonNameDataTemplate}"/>

Now we have managed to reuse a common template with only having to specify it once!  We can reference this template anywhere we know we are setting Person objects as content to all be displayed in the exact same way.

Now lets go a final step and see how we can automatically have a DataTemplate applied without even needing to reference the template itself!  Imagine that we automatically know that everywhere in the application where we are using a Person object as content that it should all be displayed this same way.  This is not a real-case scenario as there will probably not be any application where all Person objects only show the name in this format.  But their are many cases where we would want to say that one object type should always be represented in the same way.  And this is what im going to talk about  next.

So for humors sake we want all Person objects to be displayed as LastName, FirstName in the entire application.  The way we do this is by not specifying a key and instead specifying a specific data type.  We still want to put the DataTemplate in the App.xaml Resources collection so that the entire app can automatically find the template when applicable.  The code should now look something like this.

<Application x:Class="DataTemplateExample.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:models="clr-namespace:DataTemplateExample.Models"
    StartupUri="Window1.xaml">
    <Application.Resources>
        <DataTemplate DataType="{x:Type models:Person}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding LastName}"/>
                <TextBlock Text=", "/>
                <TextBlock Text="{Binding FirstName}"/>
            </StackPanel>
        </DataTemplate>
    </Application.Resources>
</Application>

We have added a namespace delcaration pointing to where we are storing our models so that we can get to the person type.  As you can see i have removed the key and instead added DataType=”{x:Type models:Person}”.  What this does is says that anytime in the entire application where an object of type Person is set as content that this DataTemplate should be used.  This is known as a global un-keyed template.  Unkeyed DataTemplates that specify a DataType will always be used when that type is set to Content.

Now that we have our global un-keyed DataTemplate this is how our ItemsControl and ContentControl would now look.

<ItemsControl ItemsSource="{StaticResource MyPersonList}"/>

<ContentControl Content="{StaticResource MySinglePerson}"/>

We have not specified any templates to be used, but the global template we have defined will automatically be used!  This is a very powerful technique that can be used accross many different aspects of WPF development. 

Lets say we wanted Themeing and in another Theme we wanted to always show FirstName, LastName.  Well we could create a second un-keyed DataTemplate and at runtime inject the new one into the Resource Tree lookup before our default and our UI will automatically change to reflect the new template!  Imagine this on a grand scale of being able to build complete lookless, themeable applications!  For more information on how the Resource lookups and Trees work please see Trees in WPF.

Global DataTemplates can also be a key player in the popular MVVM patthern for WPF.  You can create a DataTemplate representing a ViewModel so that all you have to do is create ViewModel’s and set them as content.  To see more about MVVM in WPF please see Josh Smith’s MSDN article WPF Apps With The Model-View-ViewModel Design Pattern.  Read the entire article and all code samples and you will find the DataTemplate relevance to this pattern.

Well i hope you all have enjoyed this article and learned something from it and hope you have a happy coding day!

3 Comments

Vegas Vacation

Hello All!  Time for a non-technical blog.

Last week me and my wife went on vacation to Las Vegas for the first time and wow was it alot of fun.  We got to see many shows, see the Grand Canyon, ate alot of food and got some nice relaxation in.

Our first show we saw was Danny Gans at the Encore/Wynn.  Hands-down the best show i have ever seen.  He is a true one-man variety show.  He does over 200 impersonations, sings, dances, plays instruments, does stand-up comedy and much more.  If anyone is ever in Las Vegas i would definatly recommend going to see him.

The second show we saw was KA at the MGM Grand.  This is a Cirque du Soleil show with a good kinda samurai setting.  The story is one many of us have seen but the way KA presents it is phenomenal.  There is a heavy emphasis on swordplay shown by very skilled actors.  There is definatly some really cool strangeness when you see creatures the size of humans moving around with precise style.  There were 2 very good displays of acrobatics.  I would definatly recommend seeing this show or other Cirque du Soleil shows while in Vegas.

There are other Cirque shows that were recommended that we did not get to see.  O is a heavy water based show that is supposed to be very good.  Another is Love.  This one is a Beatles themed Cirque du Soleil that has had raving reviews by both tourists and locals.

We also took a helicopter tour of the Grand Canyon.  Ive seen many pictures of the Grand Canyon but none of them do it justice.  You really have to be there to take in the wonder that it is.  The tour included flying through the canyon and landing for a half an hour for snacks and champagne and also provides good picture opportunities.

We got to stay at the Bellagio and man is it a nice hotel.  It covers a city block by itself and has many restaurants and shopping venues without ever having to leave the building.  Right now is a very good time for hotel prices.  The Bellagio is in the top 5 in Vegas and we got our room for about 150$ a night.  This is unheard of by people who have been to Vegas in the past.

Having never been to Vegas before i wasnt really sure what to expect.  Now that i have been i would definatly go back and recommend anyone seeking a fun vacation to check it out.  If you are looking for a relaxing vacation i would not recommend it.  While we did get some relaxation in, its a place that when you come home you are absolutely exhausted.

Well thats it for my vacation rant and more technical blogs on WPF are soon to come, so stay tuned!

No Comments