Win Advanced MVVM Book

Advanced MVVM CoverI am presenting a session on Building WPF Application using MVVM (Model-View-ViewModel) on May 15th at the Carolina Code Camp 2010.

I will dissect a Sample WPF MVVM application and then highlight the most important aspects of the MVVM by coding a brand new ViewModel and incorporating it into the application. You will learn about the Roles of the MVVM triad, the advantages of using MVVM and Characteristics of the ViewModel.

Attendees will be eligible to win two copies one copy (see below) of Josh Smith’s Advanced MVVM book as well as a few other WPF Books.

{ I will be giving away one hard copy version of the book instead of digital versions due to concerns about DRM protection and unclear gift guidelines at lulu.com }.

And the winner is … Mark Freeman !!! Congratulations Mark, your copy of “Advanced MVVM” is on its way. Thanks for attending the code camp and your feedback.

Using Model-View-ViewModel in WPF Applications

I am presenting at SoCalCodeCamp this weekend (January 30th, 2010) – Advanced WPF – Using Model-View-ViewModel in WPF Applications.
tasklist-mockup
tasklist-mockup

Agenda :

1. Roles of the M-V-VM triad
2. Why use M-V-VM ?
3. Characteristics of the ViewModel
4. Build a Sample WPF Application using M-V-VM



Resources :

  1. MSDN Magazine Article and Sample Application Download
  2. Martin Fowler’s description of the ‘Presentation Model ‘
  3. Jeremy Miller’s ‘Build Your Own CAB’ series

Dial Gauge is a ProgressBar

I am not just being philosophical here. In WPF, ControlTemplates allow you to transform a control’s appearance. This is not the same as applying stylesheets. This is an entirely new level of customization. What you see in the screenshot below looks like a Dial but is actually a simple ProgressBar control wearing a “Dial” template. The Dial template was created using Microsoft Expression Blend .

dial-is-a-progressbar.PNG

Include the ControlTemplate as a Window.Resource and then assign it to ProgressBar element’s Template attribute :

         

Of course that is not all. The ControlTemplate must define some named elements of FrameworkElement type. Charles Petzold has explained it in detail : MSDN Magazine Article. So, I will not repeat it here.

The sample code can be downloaded here : DialTest.zip
License : Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.

Using Custom Converter in XAML

In my previous post : WPF-LINQ To SQL Sample, I had to resort to modifying .dbml file to get the images stored as varbinary into the WPF ListView control. I had to change the Type of image fields to System.Byte[], instead of using System.Data.Linq.Binary. A better way to do this is to use a custom converter. WPF allows you to specify custom converter as part of the Binding in .xaml, like this –

<Image Grid.Row="2"
Source="{Binding Path=ThumbNailPhoto ,
Converter={StaticResource imageConverter}}"></Image>

The custom converter is declared in the same .xaml file as a resource –

<Window.Resources>
<local:ImageDataConverter x:Key="imageConverter" />
</Window.Resources>

ImageDataConverter is implemented in .xaml.cs file –

public class ImageDataConverter : IValueConverter
{
   public object Convert(object value, Type targetType, object parameter,
   System.Globalization.CultureInfo culture)
   {
      byte[] image = ((System.Data.Linq.Binary)value).ToArray();
      return image;
   }

   public object ConvertBack(object value, Type targetType, object parameter,
   System.Globalization.CultureInfo culture)
   {
      throw new NotSupportedException();
   }
}

The updated sample code can be downloaded here : WPF-LINQ to SQL Sample
License : Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.