Re: UserControl not rendering when used in DataTemplate. Bug?
I've created a test case:
1. create vs2008 silverlight application called: sl2datatemplateproblem
2. select 2nd option for application project: generate an html test page to host silverlight within this project
3. paste the following into Page.xaml:
<UserControl x:Class="sl2datatemplateproblem.Page"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sl2datatemplateproblem="clr-namespace:sl2datatemplateproblem">
<UserControl.Resources>
<sl2datatemplateproblem:MyListOfStuff x:Key="MyListOfStuffDS" />
</UserControl.Resources>
<StackPanel>
<ListBox x:Name="lstOne" MinHeight="70">
<ListBoxItem>
<ContentControl Content="unbound stuff" />
</ListBoxItem>
</ListBox>
<ListBox x:Name="lstTwo" MinHeight="70" ItemsSource="{Binding MyListOfStuffItems, Mode=OneWay, Source={StaticResource MyListOfStuffDS}}">
<ListBox.ItemTemplate>
<DataTemplate>
<ContentControl Content="{Binding ''}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox x:Name="lstThree" MinHeight="70" ItemsSource="{Binding MyListOfStuffItems, Mode=OneWay, Source={StaticResource MyListOfStuffDS}}">
<ListBox.ItemTemplate>
<DataTemplate>
<ContentControl Content="{Binding ''}" />
<!-- <sl2datatemplateproblem:MyUserControl/> -->
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</UserControl>
4. add a new usercontrol called MyUserControl.xaml and paste in the following:
<UserControl x:Class="sl2datatemplateproblem.MyUserControl"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel Orientation="Horizontal">
<ContentControl Content="{Binding ''}" />
<ContentControl Content=" [From UserControl]" />
</StackPanel>
</UserControl>
5. add a new class called MyListOfStuff.cs and paste in the following:
using System;
namespace sl2datatemplateproblem
{
public class MyListOfStuff
{
public Array MyListOfStuffItems { get; set; }
public MyListOfStuff()
{
MyListOfStuffItems = new string[] { "stuff 1", "stuff 2", "stuff 3" };
}
}
}
6. Rebuild project
7. show Page.xaml in the VS2008 xaml design view. This shows 3 listboxes, the 1st with 1 item called: "unbound stuff", the 2nd & 3rd listboxes contain 3 items from the databinding array (MyListOfStuffItems) called stuff 1, stuff 2 & stuff 3.
8. hit f5 and you get the same 3 listboxes correctly displayed in the browser.
9. in vs2008 using a split view of Page.xaml change the 3rd listbox (lstThree) by commenting out the ContentControl and uncommenting the usercontrol: <sl2datatemplateproblem:MyUserControl/>
10. reuild the project, all 3 listbox render correctly in the vs2008 xaml design view, the only difference being that the 3rd listbox is now using the UserControl and so suffixes the items with ' [From UserControl]' for clarity.
11. hit f5 and now nothing appears in the browser
12. reverse the changes in 9. (comment out the usercontrol leaving the contentcontrol uncommented), hit f5 and the listboxes reappear in the browser again.
help!
Any ideas/solutions would be greatly received as I've been banging my head for hours on this one.