简体   繁体   中英

Binding a ListBox to an ObservableCollection in XAML

I have a ListBox and a sample ObservableCollection, when I set listBox1.ItemsSource = _collection; in code-behind file, it works fine, but when I do like this in XAML:

   ItemsSource="{Binding Collection}" 

It doesn't work. In what could be the problem?

In code-behind I have

public ObservableCollection<FeedItem> Collection
{
    get { return _collection; }
}

Mostly likely the DataContext of the ListBox (or any of its parents) is not set. Bind the DataContext to the object holding the collection, and you should be good to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM