2015-05-26 63 views
1

我需要用不同的颜色以不同的方式标记ListBox中的第一个项目。如果我有listbox有没有一种方法来为它的第一个项目着色红色首选编程。ListBox特定的行/项目着色不同的颜色C#wpf

列表框:

List<CartItem> CartListItem = CartItem.getListOfCartItems(myCart, Items); 
dgProduct.ItemsSource = Items; 

的XAML

<ListBox Name="dgProduct" HorizontalContentAlignment="Stretch" Margin="0,41,10,0" Grid.RowSpan="3" 
      "> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
         . 
         . 
         . 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 

回答

1

您可以设置在ListBoxAlternationCount然后用StyleTriggerindex 0

<ListBox Name="dgProduct" 
      HorizontalContentAlignment="Stretch" 
      Margin="0,41,10,0" 
      Grid.RowSpan="3" 
      AlternationCount="1000"> 
    <ListBox.ItemContainerStyle> 
     <Style TargetType="ListBoxItem"> 
      <Setter Property="Background" Value="Transparent"/> 
      <Style.Triggers> 
       <Trigger Property="ItemsControl.AlternationIndex" Value="0"> 
        <Setter Property="Background" Value="Red"/> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </ListBox.ItemContainerStyle> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
       . 
       . 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
+0

感谢,但它不承认“背景” –

+0

@NitzanFarhi,看到编辑 –

+0

@NitzanFarhi,还属性区分大小写。确保它的“背景”而不是“背景” –