将文本文件绑定到XAML ListBox控件 [英] Binding text file to a XAML ListBox control

查看:70
本文介绍了将文本文件绑定到XAML ListBox控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Windows Metro风格的应用程序中使用C#将文本文件名绑定到ListBox控件?

How do I bind a Text File name, to a ListBox control in a Windows Metro style application using C#?

或者:

如何将Dictionary()绑定到XAML ListBox控件?

How do I bind a Dictionary() to a XAML ListBox control?

推荐答案

经过无数小时的阅读文档,搜索和提出问题之后;我设法自己解决了这个问题.

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>

隐藏代码:

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

这篇关于将文本文件绑定到XAML ListBox控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆