Sunday, September 6, 2009

Wpf : what you really need to understand first is databinding

Learning the basics layout of a WPF program is not that difficult, I mean how to place basic controls like buttons, labels, textboxes, ... Making a great interface is something else, but that requires other skills, UI designer skills. But what I think is the most important part to understand early is the binding. This is the part of the course / book that I advise you to spent the most time on. I've lost too many hours finding out why my data was not visualised :
° Compilation : 0 warnings, 0 errors
° Setting a breakpoint on the method that delivered the data : breakpoint was never hit
° Output window did not show any errors, warnings
° Settings the ItemsSource in code behind works like a charm, so the data providing method works !

Below is an example program, see if you can find the mistake.
<Window x:Class="Damn.Window1"
Title="Window1" >

<ObjectDataProvider x:Key="CCNetProject"
ObjectType="{x:Type lc:CCNetJiraDataProvider}"

<DataTemplate x:Key="CCNetProjectTemplate" >
<StackPanel Orientation="Horizontal" Background="LightBlue">
<Label Content="{Binding Key}" Width="200"/>
<Label Content="{Binding Description}" />

<StackPanel DockPanel.Dock="Top" Orientation="Vertical">
<ListBox x:Name="lstY"
ItemsSource="{Binding StaticResource CCNetProject}"
ItemTemplate="{StaticResource CCNetProjectTemplate}"/>


So this has kept me busy for some hours, ok the mistake was mine, but how could I have found the error more easily?
I can not set a breakpoint in XAML, and setting a breakpoint in the method did not get hit. How do other WPF programmers pinpoint these kind of problems. For me this is a serious problem / shortcoming in Visual Studio. Any body knows some tips/tricks ?


  1. I found a blog that has a solution

    So I'm not the only guy finding this difficult, and judging from hiw writing style, he is NOT pleased with WPF also.

  2. Found a better implementation of the binding errors detection.
    This requires a 1 extra line, and not adjusting the binding expression itself. Much easier to swithc on and off.

  3. OK - I give up!

    What is wrong with the WPF?

  4. This comment has been removed by the author.

  5. Hi, the correct binding is :
    < ListBox DockPanel.Dock="Top"
    ItemsSource="{Binding Source={StaticResource AllProjects}}"
    ItemTemplate="{StaticResource CCNetProjectTemplate}"