没有列出结果的列表框绑定

时间:2015-03-04 05:49:24

标签: xaml binding listbox windows-phone

我有一个Windows Phone应用程序,我正在尝试将ObservableCollection绑定到列表框,目前列出字符串。但是,当我运行该应用程序时,没有列出任何内容,我无法看到我出错的地方。

XAML:

                <ListBox ItemsSource="{Binding EventList}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding}" Foreground="Black" FontSize="20"/>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

代码隐藏:

    private ObservableCollection<String> eventList = new ObservableCollection<String>();

    public ObservableCollection<String> EventList
    {
        get { return eventList; }
    }

    public MainPage()
    {
        eventList.Add("Event One");
        eventList.Add("Event Two");
        eventList.Add("Event Three");

        this.InitializeComponent();

        this.NavigationCacheMode = NavigationCacheMode.Required;
    }

1 个答案:

答案 0 :(得分:0)

您是否已将主页的DataContext设置为将EventList作为属性的应用程序类?在get访问器上设置一个断点并检查它是否被调用。如果没有,那么您需要设置DataContext。