<ListBox x:Name="listBox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="10" >
<TextBlock Text="{Binding title}"/>
<TextBlock Text="{Binding Description}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
什么是正确的方式添加此源代码?Windows Phone 7从代码添加itemtemplate和datatemplate的列表框?
编辑:
尝试这样做:
public static DataTemplate createDataTemplate()
{
return (DataTemplate)System.Windows.Markup.XamlReader.Load(
@"<DataTemplate xmlns=""http://schemas.microsoft.com/client/2007"">
<TextBlock Text=""{Binding Title}"" />
<TextBlock Text=""{Binding Description}"" />
<Image Source=""{Binding Image}"" />
</DataTemplate>"
);
}
当我把这个:
for (int i=0; i<10; i++) {
ListBox lb = new ListBox();
lb.ItemTemplate = createDataTemplate();
//...then add to a pivotitem
}
我得到这个:
属性“System.Windows.FrameworkTemplate .Template'被设置多次。 [Line:3 Position:32]
为什么?
(请参见时使用大写我的你自己而不是一些变量,也请用大写字母开始句子,谢谢!) – Arjan