C# WPF ListView 改变某行某列的背景颜色

通过前端绑定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);
posted @ 2023-02-16 07:37  多见多闻  阅读(1076)  评论(0)    收藏  举报