1

我有一个绑定到 ObservableCollection 的 ListBox:

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

ObservableCollection 包含 Foo 的实例,Foo 实现了 INotifyPropertyChanged。我正在尝试将 ListBoxItems 的属性绑定到 Foo 的属性。这是我尝试过的:

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

这可行,但是,问题是我只能访问 TextBlock 的属性,而不是包含 ListBoxItem,因此,例如,Background 仅更改文本周围的颜色,而不是整个条目。我有一种感觉,我在这里使用了错误的工具来完成这项工作。

非常感谢正确方向的一点。

4

1 回答 1

1

DataTemplate指定列表框项内容的模板(UI 表示)。您需要设置样式的是项目容器本身,这可以ItemContainerStyle通过ListBox.

于 2013-08-26T16:18:26.847 回答