wpf 更改 ListViewItem 文本颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20806528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change ListViewItem text color
提问by nicoco007
I am currently creating ListViewItems programatically like so:
我目前正在以编程方式创建 ListViewItems,如下所示:
ListView.Items.Add(New With {Key .Name = ItemName, Key .DateCreated = ItemDateCreated, Key .Description = ItemDescription})
I have found no way of changing the color of the item, as I have not found a way of creating a ListViewItemwith sub items in any other way than this one.
我没有找到改变项目颜色的方法,因为我还没有找到一种除此之外的ListViewItem任何其他方式创建带有子项目的方法。
Is it possible to change the color of the item's text (or background if not possible), while still being able to have subitems?
是否可以更改项目文本的颜色(如果不可能,也可以更改背景),同时仍然可以拥有子项目?
EDIT - This is my XAML:
编辑 - 这是我的 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>
回答by BradleyDotNET
The following sample will set the first column's text to green. On a side note, you should probably wrap your "Name", "Date Created" and "Description" fields into an actual object and use it to create your ListViewItem with subitems instead of trying to create a new weakly typed object each time (I did this and attached the code if you are interested, obviously you can use any data type for members (not just string) but I was coding quickly).
以下示例将第一列的文本设置为绿色。附带说明一下,您可能应该将“名称”、“创建日期”和“描述”字段包装到一个实际对象中,并使用它来创建带有子项的 ListViewItem,而不是每次都尝试创建一个新的弱类型对象(我这样做并附上代码,如果您有兴趣,显然您可以对成员使用任何数据类型(不仅仅是字符串),但我正在快速编码)。
Information on DataTemplates: http://msdn.microsoft.com/en-us/library/system.windows.datatemplate(v=vs.110).aspx
关于数据模板的信息:http: //msdn.microsoft.com/en-us/library/system.windows.datatemplate(v=vs.110) .aspx
XAML:
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#:
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;
}
}

