问题
How do I bind a Text File name, to a ListBox control in a Windows Metro style application using C#?
Or:
How do I bind a Dictionary() to a XAML ListBox control?
回答1:
After what seemed to be endless hours of reading through documentation, searching, and asking questions; I managed to figure this out for myself.
Below is the code that works just as I need it to in my situation:
XAML:
<ListBox Name="NotesList">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Filename}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Code-behind:
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;
}
来源:https://stackoverflow.com/questions/12420783/binding-text-file-to-a-xaml-listbox-control