Ok, I decided to โcrackโ the solution a bit, but it works.
Since it seems that XamlReader does not know about the local namespace when creating the DataTemplate, I expanded the StackPanel and โbakeโ in the Load event. This is not entirely ideal, but it works:
this._listBox.ItemTemplate = (DataTemplate) XamlReader.Load( @"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:foo=""clr-namespace:Foo;assembly=Foo""> <Border> <foo:ExtendedStackPanel> <TextBlock Text=""{Binding " + this._displayMemberPath + @"}"" /> </foo:ExtendedStackPanel> </Border> </DataTemplate>" );
And the extended class:
public class ExtendedStackPanel : StackPanel { public ExtendedStackPanel() : base() { this.Loaded += new RoutedEventHandler(this.ChildItem_Loaded); } private void ChildItem_Loaded(object sender, RoutedEventArgs e) {
source share