Binding text file to a XAML ListBox control

拈花ヽ惹草 提交于 2019-12-13 20:04:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!