通过前端绑定Background属性实现,代码如下:
<ListView x:Name="listView1" ItemsSource="{Binding items}" Width="641" Margin="0,0,0,-5" ItemContainerStyle="{StaticResource ListViewItemStyle}">
<ListView.View>
<GridView>
<GridViewColumn Header=" 序号" Width="130">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Width="110" TextAlignment="Center" Background="{Binding numberBackgroud}" Text="{Binding number}"></TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header=" 名称" Width="110">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Width="110" TextAlignment="Center" Background="{Binding nameBackgroud}" Text="{Binding name}"></TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
public class DataClass
{
public string number { get; set; }
public string numberBackground { get; set; }
public string name { get; set; }
public string nameBackground { get; set; }
}
List<ListViewItem> ITEMS = new List<ListViewItem>();
// 资源绑定
listView1.ItemsSource = ITEMS;
ListViewItem one_item = new ListViewItem();
one_item.Content = new DataClass()
{
number = "11",
name = "hello_world",
numberBackground = "red",
nameBackground = "Yellow",
};
ITEMS.Add(one_item);
标签:set,某列,get,C#,ITEMS,new,WPF,public,string
From: https://www.cnblogs.com/guangzhiruijie/p/17125360.html