tbs*_*asa 8 c# visual-studio windows-phone-7
我的xaml文件
<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>
Run Code Online (Sandbox Code Playgroud)
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:
}
Run Code Online (Sandbox Code Playgroud)
Taskonlistbox:
public class Taskonlistbox
{
public string Text { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
错误:"在使用ItemsSource之前,项目集合必须为空""是什么问题?
Oli*_*bes 13
您只想创建一次列表并仅分配数据源一次!因此,创建列表之前的循环,并指定数据源后循环
// Clear the listbox.
// If you never add items with listBox1.Items.Add(item); you can drop this statement.
listBox1.Items.Clear();
// Create the list once.
List<Taskonlistbox> dataSource = new List<Taskonlistbox>();
// Loop through the tasks and add items to the list.
for (int i = 0; i < tasks.Count(); i++) {
dataSource.Add(new Taskonlistbox {Text = "Blalalalala"} );
}
// Assign the list to the `ItemsSouce` of the listbox once.
this.listBox1.ItemsSource = dataSource;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
27784 次 |
| 最近记录: |