<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