解决方案非常简单:
如果您想为所有项目设置相同的颜色,那么以下方式将是合适的:
<ListView Height="768" Width="220" Background="Silver">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Foreground" Value="Blue"></Setter>
</Style>
</ListView.ItemContainerStyle>
<system:String>Item 1</system:String>
<system:String>Item 2</system:String>
<system:String>Item 3</system:String>
</ListView>
为了使特定项目具有唯一的颜色,您应该AlternationCount
为 ListView 本身放置属性:
<ListView AlternationCount="50" Height="768" Width="220" Background="Silver">
和内的触发器ItemContainerStyle
。这里的 TriggerValue
属性是项目的索引:
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Foreground" Value="Red"></Setter>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Foreground" Value="Blue"></Setter>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="2">
<Setter Property="Foreground" Value="Black"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
此时很容易弄清楚如何操作Foreground
.