![](/img/trans.png)
[英]Binding to ListBoxItem properties in ListBox ItemContainerStyle
[英]Binding ListBoxItem properties when ListBox is bound to ObservableCollection
我有一个绑定到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仅更改文本周围的颜色,而不更改整个条目的颜色。 我觉得我在这里使用错误的工具进行工作。
朝着正确方向的观点将不胜感激。
DataTemplate
指定列表框项目的内容的模板(UI演示)。 您需要样式化的是项目容器本身,可以通过ListBox
的ItemContainerStyle
属性来完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.