我有一个ListView,在其中我显示给定类型的对象列表。对象的一个属性是颜色。有没有办法为给定颜色的每一行着色,或者以任何图形方式显示它?现在,我只想出了如何在单独的列中显示颜色字符串。 对象:
public class Parcel
{
public int parcelId { get; set; }
public int sizeX { get; set; }
public int sizeY { get; set; }
public int positionX { get; set; }
public int positionY { get; set; }
public int area { get; set; }
public Color color { get; set; }
private Random rnd = new Random();
public Parcel(int id, int sX, int sY, int pX, int pY)
{
parcelId = id;
sizeX = sX;
sizeY = sY;
positionX = pX;
positionY = pY;
area = sX * sY;
color = Color.FromRgb((byte)rnd.Next(0, 256), (byte)rnd.Next(0, 256), (byte)rnd.Next(0, 256));
}
}
ListView:
<ListView x:Name="listViewParcelsInContainer" HorizontalAlignment="Left" Height="201" Margin="115,309,0,0" VerticalAlignment="Top" Width="294">
<ListView.View>
<GridView>
<GridViewColumn Header="ID PACZKI" DisplayMemberBinding="{Binding parcelId}"/>
<GridViewColumn Header="Szerokość (w cm)" DisplayMemberBinding="{Binding sizeX}"/>
<GridViewColumn Header="Długość (w cm)" DisplayMemberBinding="{Binding sizeY}"/>
<GridViewColumn Header="Powierzchnia(w cm^2)" DisplayMemberBinding="{Binding area}"/>
<GridViewColumn Header="Kolor" DisplayMemberBinding="{Binding color}"/>
</GridView>
</ListView.View>
</ListView>
答案 0 :(得分:0)
使用ListViewItem
样式:
<ListView x:Name="listViewParcelsInContainer" HorizontalAlignment="Left" Height="201" Margin="115,309,0,0" VerticalAlignment="Top" Width="294">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="{Binding color}"/>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Header="ID PACZKI" DisplayMemberBinding="{Binding parcelId}"/>
<GridViewColumn Header="Szerokość (w cm)" DisplayMemberBinding="{Binding sizeX}"/>
<GridViewColumn Header="Długość (w cm)" DisplayMemberBinding="{Binding sizeY}"/>
<GridViewColumn Header="Powierzchnia(w cm^2)" DisplayMemberBinding="{Binding area}"/>
<GridViewColumn Header="Kolor" DisplayMemberBinding="{Binding color}">
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>