【问题标题】:Binding text file to a XAML ListBox control将文本文件绑定到 XAML ListBox 控件
【发布时间】:2012-09-14 08:33:18
【问题描述】:

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

或者:

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

【问题讨论】:

    标签: c# wpf xaml data-binding microsoft-metro


    【解决方案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;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多