8
我的XAML文件之前,項集合必須爲空錯誤:使用的ItemsSource
<ListBox Height="522" HorizontalAlignment="Left" Margin="20,162,0,0" Name="listBox1" VerticalAlignment="Top" Width="448" ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Text}" Foreground="#FFC8AB14" FontSize="36" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
xaml.cs文件
listBox1.Items.Clear();
for (int i = 0; i < tasks.Count(); i++) {
List<Taskonlistbox> dataSource = new List<Taskonlistbox>();
dataSource.Add(new Taskonlistbox() {Text = "Blalalalala"});
this.listBox1.ItemsSource = dataSource; // visual stdio shows error here:
}
Taskonlistbox:
public class Taskonlistbox
{
public string Text { get; set; }
}
錯誤:「Items集合必須爲空之前使用ItemsSource「 最新問題?
這似乎與http://stackoverflow.com/questions/683863/wpf-items-collection-must -be-empty-before-using-itemssource – Chriseyre2000 2012-02-12 18:26:19
可能不是根錯誤,但不應該在for循環中設置ItemsSource。 – 2012-02-12 18:28:47