如何根据项属性修饰ListView行?

时间:2011-05-27 11:33:15

标签: wpf xaml data-binding styles

我将项目列表绑定到WPF ListView,并希望根据项目属性值设置行背景/前景。我有这样的XAML但颜色没有应用:

<ListView x:Name="lvItems">
  <ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
      <Setter Property="Foreground" Value="{Binding Path=Color}"/>
    </Style>
  </ListView.ItemContainerStyle>

  <ListView.View>
    <GridView>
      <GridViewColumn DisplayMemberBinding="{Binding Path=Description}" Header="Description"/>
    ...

lvItems.ItemsSource = list of { Description, Color } 

我做错了什么?

1 个答案:

答案 0 :(得分:2)

你不能将颜色绑定到画笔属性(如果你的Color属性实际上是一种颜色),它应该是这样的:

<Setter Property="Foreground">
    <Setter.Value>
        <SolidColorBrush Color="{Binding Color}" />
    </Setter.Value>
</Setter>