我们如何在运行时向 Windows Metro 风格应用程序中的 ListBox 控件添加新项目?
我来自 WinForms,所以你可以想象,我现在很困惑。
我有以下内容:
public class NoteView
{
public string Title { get; set; }
public string b { get; set; }
public string c { get; set; }
}
接着:
List<NoteView> notes = new List<NoteView>();
protected void Button1_Click(object sender, RoutedEventArgs e)
{
notes.Add(new NoteView {
a = "text one",
b = "whatevs",
c = "yawns"
});
NotesList.ItemsSource = notes;
}
这是没用的。它什么也不做。此外,“输出”窗口中没有任何内容。没有错误,没有例外;没有什么。
所以,然后我尝试直接添加到ListBox:
NotesList.Items.Add("whatever!");
再一次,什么也没发生。所以然后我尝试添加UpdateLayout();
,但这也没有帮助。
有人知道这是怎么回事吗?
我们如何向 XAML 列表框添加新项目?
更新:
<ListBox Name="NotesList" Background="WhiteSmoke">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Title, Mode=TwoWay}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>