Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change ListViewItem text color

Tags:

wpf

I am currently creating ListViewItems programatically like so:

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 ListViewItem with sub items in any other way than this one.

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:

<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>
like image 798
Nicolas Gnyra Avatar asked Mar 22 '23 04:03

Nicolas Gnyra


1 Answers

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).

Information on 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;
    }
}
like image 106
BradleyDotNET Avatar answered Apr 07 '23 19:04

BradleyDotNET