pseudoDust
pseudoDust

Reputation: 1386

Binding ListBoxItem properties when ListBox is bound to ObservableCollection

I have a ListBox bound to an ObservableCollection:

<ListBox ItemsSource="{Binding ObservableCollectionOfFoos}" />

The ObservableCollection contains instances of Foo, Foo implements INotifyPropertyChanged. I'm trying to bind properties of the ListBoxItems to properties of Foo. Here is what I tried:

<DataTemplate DataType="{x:Type local:Foo}" >
    <TextBlock Content="{Binding PropertyOfFoo}" Background="{Binding AnotherPropertyOfFoo}"/>
</DataTemplate>

this works, but, the problem is that I only have access to the properties of the TextBlock and not the containing ListBoxItem, so, for example, Background only changes the color around the text and not the whole entry. I have a feeling I'm using the wrong tool for the job here.

a point in the right direction would be very appreciated.

Upvotes: 1

Views: 89

Answers (1)

Mike Dinescu
Mike Dinescu

Reputation: 55720

The DataTemplate specifies the template (UI presentation) of the content of the list box item. What you need to style is the item container itself which can be done via the ItemContainerStyle property of the ListBox.

Upvotes: 1

Related Questions