我有以下ListView
:
<ListView>
<ListView.View>
<GridView/>
</ListView.View>
</ListView>
我想以编程方式将第二个行(例如)的背景设置为红色。 我该怎么办? 感谢。
答案 0 :(得分:3)
您可以使用类似的方法:
int index = 1;
ListViewItem row = ListView.ItemContainerGenerator.ContainerFromIndex(index) as ListViewItem;
row.BackGround = Brushes.Red;
答案 1 :(得分:2)
您可以使用AlternationCount
在AlternationIndex
中存储行的索引,然后使用触发器将第二行(索引1)的背景设置为红色:
<ListView AlternationCount="{x:Static sys:Int32.MaxValue}">
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
...
</ListView>
AlternationCount
定义重新启动到零之前要计数的行数。例如,如果设置为2,则行的索引将为0 1 0 1 0 1 ...,允许您仅绘制奇数行或偶数行的红色。将其设置为MaxValue
将永远不会重新开始计数,从而有效地将AlternationIndex
设置为实际索引。
答案 2 :(得分:-1)
请在这里澄清一件事。你能告诉我你是否想把第二行的颜色变成红色。如果是这种情况,您可以遍历列表视图,然后在顶部保持计数变量,然后当count == 1时,您可以将列表view.background颜色更改为红色。如果你试过这个,那么你能告诉我发生了什么吗?它是以错误结束还是什么或什么也没发生。