views:

30

answers:

2

Simple Silverlight question: I have an ObservableCollection<MyObject> in my viewmodel. Every MyObject has a Label property. If I bind a ListBox to the collection and set DisplayMemberPath to Label, or set the ItemTemplate to a TextBlock that binds the Text property to Label, all works as expected.

If I change MyObject so it derives from a UserControl, the Label text no longer shows up in the ListBox; each item just shows up as a blank strip a few pixels tall. Why is this? There's obviously something I'm missing here about how different things get rendered.

+2  A: 

The ListBox determines that the set of items in its ItemsSource are already UIElement instances and therefore decides to use those elements directly as the content of the ListBoxItem elements it creates.

AnthonyWJones
Thanks. It's this kind of thing that frustrates me about XAML - I can give it very specific instructions to render the content in a certain way and it completely ignores them!
nlawalker
nlawalker: Either they didn't anticipate this scenario or perhaps they felt the cost of working round this scenario wasn't woth the effort. After all it is quite strange to take a set of UIElements and treat them as data
AnthonyWJones
A: 

You can change this behavior by creating your own ItemsControl, overriding IsItemItsOwnContainerOverride, as explained in the answer to this post.

Tosh