65.9K
CodeProject is changing. Read more.
Home

WPF: How to highlight a cell in ListView

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.33/5 (12 votes)

Jul 16, 2008

CPOL

3 min read

viewsIcon

42045

downloadIcon

560

A solution to highlight a cell in ListView

Download HighlightListView.zip - 20.47 KB

Thanks Code Project

In my whole career of software developing I have go through this site thousand times as many developers in this whole world. This is the time I want to contribute rather share my first article.

Introduction

This is my first article in WPF as well as C#. In WPF you can highlight entire row in ListView, but to highlight a particular cell there is a twist there.

Background

It started when I was trying to learn WPF, answering a post what seems to be an easy one turns to be as daunting one. To highlight a particular cell you have to take some approach rather just populating the list view.

In this article the ListView is populated in two ways, one is using a collection class another one if user has SQL Server there the can populate the list view. Collection class is for my own interest in which I am facing more difficulties to highlight a cell. OK reason may be I am a beginner in WPF.

Using the code

OK you have created an application where there is a ListView which is being populated from your Employeeinfo database. Now all the three fields FirstName, LastName & ID are blinded to the ListView.

You requirement is that you want to highlight your specific search item. For this you create style but no result as it highlights the entire row not a particular cell. To cut the long story short as well as applying style you have to create a user control textbox which will be there for each cell, you can easily highlight that textbox.

Now your XAML file will look something like this.

<ListView Name="ListViewEmployeeDetails" Margin="22,17,16,161" ItemTemplate="{DynamicResource EmployeeTemplate}" ItemsSource="{Binding Path=Table}"> <ListView.Background> <LinearGradientBrush> <GradientStop Color="Bisque" Offset="0"/> </LinearGradientBrush> </ListView.Background> <ListView.View> <GridView>

<!--<GridViewColumn Header="First Name" Width ="100" DisplayMemberBinding="{Binding Path=FirstName}"/>
<GridViewColumn Header="Last Name" Width ="100" DisplayMemberBinding="{Binding Path=LastName}"/>
<GridViewColumn Header="ID" Width ="100" DisplayMemberBinding="{Binding Path=ID}"/>-->

<GridViewColumn Header="First Name" Width ="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<l:UserEditBox Height="25" Value="{Binding Path=FirstName}" Loaded="EditBox_Loaded"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Last Name" Width ="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<l:UserEditBox Height="25" Value="{Binding Path=LastName}" Loaded="EditBox_Loaded"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="ID" Width ="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<l:UserEditBox Height="25" Value="{Binding Path=ID}" Loaded="EditBox_Loaded"/> </DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>

</GridView>
</ListView.View>
</ListView>

This is the code which will populate the ListView replacing the cell with UserEditBox which is a user control playing a vital role to highlight the back ground. I have changed the foreground color, also is changing the font size & making it bold.

Now your UserEditBox is having two dependency properties one is ValueProperty & other one is BackgroundProperty. In your handler "EditBox_Loaded" you can check for the search criteria & highlight that cell.

To test the other case where the data is stored in a collection class, you can change the StartupUri = "EmployeeInfo.xaml". In this case the cell is not highlighted; currently I am investigating how to implement the exact functionality.

Now the solution to the collection data is also solved. Please have a look at

Employeeinfo.xaml &

Employeeinfo.xaml.cs

History

First published:

16-07-2008