[WPF] Article about the Model-View-ViewModel design pattern, by Josh Smith

Soon after the release of WPF, people have been talking more and more about “Model-View-ViewModel” (MVVM). This expression refers to a design pattern, drawing its inspiration from the Model-View-Controller (MVC) and Presentation Model (PM) patterns, and created specifically to take advantage of WPF features. This patterns enables an excellent decoupling between data, behavior and presentation, which makes the code easier to understand and maintain, and improves the collaboration between developers and designers. Another benefit of MVVM is the ability to write testable code much more easily.

If you want to know more about this pattern, I urge you to read the excellent article by Josh Smith on this topic, published in the February issue of the MSDN Magazine : WPF Apps With The Model-View-ViewModel Design Pattern.

Walking through a simple but concrete example, Josh Smith addresses most aspects of the MVVM pattern :

  • Data binding
  • Commands
  • Validation
  • Unit testing

Further more, the provided source code makes a good starting point to build a WPF application conforming to the MVVM pattern, and is also a mine of practical examples.

Build an RSS reader in 5 minutes

Today, I stumbled upon a very handy class : SyndicationFeed. This class, introduced in .NET 3.5, allows to manipulate syndication feeds (like RSS 2.0 or Atom 1.0) with very little code. It can be used to create and publish our own feeds, or to read existing ones.

For instance, here’s how to retrieve the news feed from Google News and display its title, its hyperlink, and the titles of it’s items :

string url = "http://news.google.fr/nwshp?hl=fr&tab=wn&output=rss";
using (XmlReader reader = XmlReader.Create(url))
    SyndicationFeed feed = SyndicationFeed.Load(reader);
    foreach(SyndicationItem item in feed.Items)

Easy enough, don’t you think ? 🙂

Let’s now take advantage of WPF binding capabilities to create a very simple graphical RSS reader :

<Window x:Class="TestFeeds.Window1"
        Title="Minimalist feed reader" Height="286" Width="531">
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        <DockPanel Grid.Row="0">
            <Button Name="btnGo"
                    Click="btnGo_Click" />
            <TextBox Name="txtUrl" />
        <Grid Grid.Row="1">
                <ColumnDefinition Width="250"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            <ListBox Name="lstFeedItems"
                     DisplayMemberPath="Title.Text" />
            <GridSplitter Grid.Column="1"
            <Frame Name="frmContents"
                   Source="{Binding SelectedItem.Links[0].Uri, ElementName=lstFeedItems}"

The code-behind :

    private void btnGo_Click(object sender, RoutedEventArgs e)
        using (XmlReader reader = XmlReader.Create(txtUrl.Text))
            SyndicationFeed feed = SyndicationFeed.Load(reader);
            lstFeedItems.ItemsSource = feed.Items;

And here’s the result !


[WPF] Paste an image from the clipboard (bug in Clipboard.GetImage)

Oops… 2 months already since my previous (and first) post… I really have to get on a more regular schedule 😉

If you’ve ever tried to use the Clipboard.GetImage method in WPF, you probably had an unpleasant surprise… In fact, this method returns an InteropBitmap which, in some cases (most cases actually), can’t be displayed in an Image control : no exception is thrown, the image size is correct, but the image either appears empty or unrecognizable.

However, if we save that image to a stream and re-read it from the stream, we get a perfectly usable image… So this could be an acceptable workaround, but I think its pretty bad for performance, because the image gets decoded, re-encoded, and re-decoded. It is also possible to use the Clipboard class from Windows Forms, which works fine, and convert the System.Drawing.Image to a System.Windows.Media.ImageSource, but I don’t like the idea of referencing the Windows Forms assembly in a WPF app… So I decided to manually retrieve the image from the clipboard and handle the decoding myself.

If we look at the image formats available from the clipboard (Clipboard.GetDataObject().GetFormats()), we can see that they depend on the origin of the image (screenshot, copy from Paint…). The only format that is always available is DeviceIndependentBitmap (DIB). So I tried to retrieve the MemoryStream for this format and decode it into a BitmapSource :

        private ImageSource ImageFromClipboardDib()
            MemoryStream ms = Clipboard.GetData("DeviceIndependentBitmap") as MemoryStream;
            BitmapImage bmp = new BitmapImage();
            bmp.StreamSource = ms;
            return bmp;

Unfortunately, this code throws a nasty NotSupportedException : « No imaging component suitable to complete this operation was found ». In other words, it doesn’t know how to decode the contents of the stream… That’s quite surprising, because DIB is a very common format. So I had a look at the structure of a DIB in MSDN documentation. Basically, a « classical » bitmap file (.bmp) is made of the following sections :

  • File header (BITMAPFILEHEADER structure)
  • Bitmap header (BITMAPINFO structure)
  • Palette (array of RGBQUAD)
  • Raw pixel data

If we observe the content of the DIB from the clipboard, we can see that it has the same structure, without the BITMAPFILEHEADER part… so the trick is just to add that header at the beginning of the buffer, and use this complete buffer to decode the image. Doesn’t seem so hard, does it ? Well, the trouble is that we have to fill in some of the header fields… for instance, we must provide the location at which the actual image data begins, so we must know the total size of the headers and palette. These values can be read or calculated from the content of the image. The following code performs that task and returns an ImageSource from the clipboard :

        private ImageSource ImageFromClipboardDib()
            MemoryStream ms = Clipboard.GetData("DeviceIndependentBitmap") as MemoryStream;
            if (ms != null)
                byte[] dibBuffer = new byte[ms.Length];
                ms.Read(dibBuffer, 0, dibBuffer.Length);

                BITMAPINFOHEADER infoHeader =

                int fileHeaderSize = Marshal.SizeOf(typeof(BITMAPFILEHEADER));
                int infoHeaderSize = infoHeader.biSize;
                int fileSize = fileHeaderSize + infoHeader.biSize + infoHeader.biSizeImage;

                BITMAPFILEHEADER fileHeader = new BITMAPFILEHEADER();
                fileHeader.bfType = BITMAPFILEHEADER.BM;
                fileHeader.bfSize = fileSize;
                fileHeader.bfReserved1 = 0;
                fileHeader.bfReserved2 = 0;
                fileHeader.bfOffBits = fileHeaderSize + infoHeaderSize + infoHeader.biClrUsed * 4;

                byte[] fileHeaderBytes =

                MemoryStream msBitmap = new MemoryStream();
                msBitmap.Write(fileHeaderBytes, 0, fileHeaderSize);
                msBitmap.Write(dibBuffer, 0, dibBuffer.Length);
                msBitmap.Seek(0, SeekOrigin.Begin);

                return BitmapFrame.Create(msBitmap);
            return null;

Definition of the BITMAPFILEHEADER and BITMAPINFOHEADER structures :

        [StructLayout(LayoutKind.Sequential, Pack = 2)]
        private struct BITMAPFILEHEADER
            public static readonly short BM = 0x4d42; // BM

            public short bfType;
            public int bfSize;
            public short bfReserved1;
            public short bfReserved2;
            public int bfOffBits;

        private struct BITMAPINFOHEADER
            public int biSize;
            public int biWidth;
            public int biHeight;
            public short biPlanes;
            public short biBitCount;
            public int biCompression;
            public int biSizeImage;
            public int biXPelsPerMeter;
            public int biYPelsPerMeter;
            public int biClrUsed;
            public int biClrImportant;

Utility class to convert structures to binary :

    public static class BinaryStructConverter
        public static T FromByteArray<T>(byte[] bytes) where T : struct
            IntPtr ptr = IntPtr.Zero;
                int size = Marshal.SizeOf(typeof(T));
                ptr = Marshal.AllocHGlobal(size);
                Marshal.Copy(bytes, 0, ptr, size);
                object obj = Marshal.PtrToStructure(ptr, typeof(T));
                return (T)obj;
                if (ptr != IntPtr.Zero)

        public static byte[] ToByteArray<T>(T obj) where T : struct
            IntPtr ptr = IntPtr.Zero;
                int size = Marshal.SizeOf(typeof(T));
                ptr = Marshal.AllocHGlobal(size);
                Marshal.StructureToPtr(obj, ptr, true);
                byte[] bytes = new byte[size];
                Marshal.Copy(ptr, bytes, 0, size);
                return bytes;
                if (ptr != IntPtr.Zero)

The image returned by that code can be safely used in an Image control.

That goes to show that, even with a state-of-the-art technology like WPF, we still have to get our hands dirty sometimes ;). Let’s hope Microsoft will fix this in a later version…