Sunday, June 6, 2010

WPF 3.5 Bug in inheriting DataTemplate

You can easily reproduce the bug.Just inherit the DataTemplate and use the new DataTemplate class instead of default.You will get an runtime exception.

Your DataTemplate class

public class MyDataTemplate :DataTemplate
{
}


Nothing unusual rite? Then use this DataTemplate in your XAML.

<ListBox x:Name="lstPersons" >
<ListBox.ItemTemplate>
<local:MyDataTemplate >
<StackPanel>
<Button Content="{Binding Name}" />
</StackPanel>
</local:MyDataTemplate>
</ListBox.ItemTemplate>
</ListBox>


Just using the new class.But when we run this we get the exception as follows.


StackPanel' object cannot be added to 'MyDataTemplate'. Object of type 'System.Windows.Controls.StackPanel' cannot be converted to type 'System.Windows.FrameworkElementFactory.

This is true same in the case of HierarchicalDataTemplate too.

This is an identified bug.Microsoft has fixed this and WPF 4 contains the fix.

No comments:

Post a Comment