我目前正以编程方式创建ListViewItems:
ListView.Items.Add(New With {Key .Name = ItemName, Key .DateCreated = ItemDateCreated, Key .Description = ItemDescription})
我发现没有办法改变项目的颜色,因为我还没有找到一种方法来创建一个带有子项目的ListViewItem
,而不是以这个方式。
是否可以更改项目文本(或背景,如果不可能)的颜色,同时仍然可以有子项目?
编辑 - 这是我的XAML:
<ListView x:Name="ListView" Opacity="0.75">
<ListView.View>
<GridView x:Name="ListViewGridView">
<GridViewColumn Header="Name" Width="155" DisplayMemberBinding="{Binding Path=Name}"/>
<GridViewColumn Header="Date Created" Width="150" DisplayMemberBinding="{Binding Path=DateCreated}"/>
<GridViewColumn Header="Description" Width="250" DisplayMemberBinding="{Binding Path=Description}"/>
</GridView>
</ListView.View>
</ListView>
答案 0 :(得分:5)
以下示例将第一列的文本设置为绿色。在旁注中,您应该将“Name”,“Date Created”和“Description”字段包装到实际对象中,并使用它来创建带有子项的ListViewItem,而不是每次都尝试创建一个新的弱类型对象(I如果您感兴趣,这样做并附上代码,显然您可以为成员使用任何数据类型(不仅仅是字符串),但我很快就编码了。
有关DataTemplates的信息: http://msdn.microsoft.com/en-us/library/system.windows.datatemplate(v=vs.110).aspx
XAML:
<ListView x:Name="ListView" Opacity="0.75">
<ListView.View>
<GridView x:Name="ListViewGridView">
<GridViewColumn Header="Name" Width="155">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}" Foreground="{Binding Color}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Date Created" Width="150" DisplayMemberBinding="{Binding Path=DateCreated}"/>
<GridViewColumn Header="Description" Width="250" DisplayMemberBinding="{Binding Path=Description}"/>
</GridView>
</ListView.View>
</ListView>
C#:
// In method somewhere, won't work otherwise
ListView.Items.Add(new DataClass("Bob", "12/04/2013", "Person", Color.Green));
public class DataClass
{
public String Name { get; set; }
public String DateCreated { get; set; }
public String Description { get; set; }
public SolidBrushColor { get; set; }
public DataClass (string Name, String Date, String Desc, SolidBrushColor textColor)
{
this.Name = Name;
this.DateCreated = Date;
Description = Desc;
Color = textColor;
}
}