ListBox 特定 row/item 着色 Diffrent Color C# wpf
ListBox specific row/item coloring Diffrent Color C# wpf
我需要用不同的方式标记列表框中的第一项,比如用不同的颜色。如果我有 listbox
有没有办法以编程方式将其中的第一项着色为红色首选。
列表框:
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>
您可以在 ListBox
上设置 AlternationCount
,然后对 index 0
使用 StyleTrigger
。
<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>
我需要用不同的方式标记列表框中的第一项,比如用不同的颜色。如果我有 listbox
有没有办法以编程方式将其中的第一项着色为红色首选。
列表框:
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>
您可以在 ListBox
上设置 AlternationCount
,然后对 index 0
使用 StyleTrigger
。
<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>