Friday, October 2, 2009

Wpf : style issues

Got the program almost like I want it. Encountered another big annoying WPF issue :
the order of the styles and datatemplates in a XAML file is of importance !
Take a look at the following example :
<DataTemplate x:Key="IssueDataTemplate" >
<ContentControl Style="{StaticResource IssueTemplate}"/>
</DataTemplate >

<Style x:Key="IssueTemplate" >
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<StackPanel Orientation="Horizontal" >
<Label Content="{Binding Key}" Width="200"/>
<Label Content="{Binding Description}" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
You get a freaking runtime error : Unable to find style IssueTemplate ! ! !

To fix it, place the style first in the file, and next the datatemplate.

Questions :
1) Why does it throw a runtime error, and not a compile error, it is a static resource, meaning that it is known at design time, it is not loaded via an assembly at runtime (late binding technology).
2) Why an error in the first place? Back in 1990, it was of importance to place the procedures in correct order in the code, so the compiler could find them. But we're now in 2009! This is the way of working of 20 years ago!

When you code in any .Net language, Delphi, C, VB6, VB5, VB4 or even COBOL the place of a function in a class does not matter, hey even with partial classes, it does not even matter in which file they are! But WPF fails if the order is not ok.

I have a feeling that this issue will make the style files unorganized, and so making it errorprone :-(

sigh me wonder what is so fun and exciting about wpf, can someone enlighten me ? I do not grasp it.

4 comments:

  1. Hi,

    You can simply use DynamicResource instead of StaticResource to avoid this issue !

    Cheers,

    Roland Tomczak

    ReplyDelete
  2. Thanks for the pointer !
    I did some more research about the difference between static and dynamic resource, and found a quote on some blog :Actually the semantics for a static resource are check once AND MAKE SURE IT IS THERE at the launch of the executable otherwise throw an exception. Dynamic resources do not check until set to a dynamic resource and if you can’t find it don’t worry about it.
    Ok I understand the difference, but that still does not explain why the order in the file is of importance. As far as I know, style sheets are loaded/parsed as a whole, and not only the part that is needed, like with code(JIT). Reading and thinking this through gives me the shakes. This sounds a lot like CSS, but now CSS on steroids : even more hard to track issues.

    ReplyDelete
  3. Just put the content in the content control directly in data template.

    ReplyDelete
  4. That would solve the problem, but means that the layout is defined twice, something I would not like to do. As with writing code, code once, and call X times. Not code X times. Maybe that is my problem with WPF, I treat it as a programming language, while it should not be threated like that. Anyway WPF keeps surprising me in a negative way ...

    ReplyDelete