简体   繁体   English

ListBox特定的行/项目着色Diffrent Color C#wpf

[英]ListBox specific row/item coloring Diffrent Color C# wpf

I need to Mark the first item in the ListBox in a different way like in a different color. 我需要以不同的方式(例如用不同的颜色)标记ListBox中的第一项。 if i have listbox is there a way to color the first item in it Red preferred programmatically. 如果我有listbox ,有没有办法以编程方式为它的第一个红色上色。

ListBox: 列表框:

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

Xaml XAML

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

You can set the AlternationCount on the ListBox and then use a StyleTrigger for index 0 . 您可以在ListBox上设置AlternationCount ,然后将StyleTrigger用作index 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM