绑定文本文件到XAML ListBox控件(Binding text file to a XAML

2019-10-17 05:20发布

如何绑定一个文本文件的名称,在使用C#一个Windows Metro风格的应用程序列表框控件?

要么:

如何绑定一个Dictionary()的XAML ListBox控件?

Answer 1:

经过了似乎是通读文件,搜索和提问无数的时间; 我设法弄清楚这一点为自己。

下面是工作只是因为我需要它在我的情况的代码:

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;
    }


文章来源: Binding text file to a XAML ListBox control