We found a nasty bug today in WPF involving the use of a datatemplate selector.
Here is the jist of it in an example.
If you have a listbox and specify a datatemplate for it directly you can then retrieve that datatemplate in the code behind by simply saying Listbox1.ItemTemplate. This gives you your item template which you can then traverse to customize elements in that template in your code behind which is a pretty common thing to do.
However, if you are using a DataTemplateSelector to choose the template for an object (we'll continue to use a listbox as an example) you will see that even though visually the template has been applied, in the code behind when you try to retrieve the item template by utilizing Listbox1.ItemTemplate you get a value of "Nothing."
We had a very complex WPF scenario and we assumed that the fact that we were getting nothing returned to us was due to some error in our thinking or logic. After many hours we were able to determine that the problem was with WPF and this inconsistency.
Attached is a very simple sample application that display's the issue in a very clear fashion.
Please note that I found this article which has a workaround that can be dangerous to use. The article specifies adding a property to your DataTemplateSelector that stores the last template selected. This can be very dangerous as if another control calls this DataTemplateSelector the value returned will not be what you expect.
Hopefully this will be fixed in .net 4.0. In the meantime I will post this on the MS Connect site to see if they can come up with a resolution. Learning WPF can be very difficult, when it doesn't behave in a stable fashion it becomes very frustrating.
Sean
VB.net sample project illustrating template selector bug.