如何使用 C# 将文本文件名绑定到 Windows Metro 风格应用程序中的 ListBox 控件?
或者:
如何将 Dictionary() 绑定到 XAML ListBox 控件?
如何使用 C# 将文本文件名绑定到 Windows Metro 风格应用程序中的 ListBox 控件?
或者:
如何将 Dictionary() 绑定到 XAML ListBox 控件?
经过似乎无休止地阅读文档、搜索和提问的时间;我设法自己解决了这个问题。
下面是在我的情况下可以正常工作的代码:
XAML:
<ListBox Name="NotesList">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Filename}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
代码隐藏:
public class NoteView
{
public string Filename { get; set; }
public string Contents { get; set; }
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
var noteList = new ObservableCollection<NoteView>();
for (int i = 0; i < 10; i++)
{
noteList.Add(new NoteView { Filename = "Sample note " + i });
}
NotesList.ItemsSource = noteList;
}