Click here to Skip to main content
Click here to Skip to main content

WPF: How to highlight a cell in ListView

By , 15 Jul 2008
 

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Yajnesh Narayan Behera
Software Developer (Senior)
India India
Member
I am a software developer holding Master degree in computer science. I have expertise in C++, VC++, ATL(COM), C#, OpenSSL, Shell Script, STL, UML, DirectShow, Vanguard (VSoft SDK), DLNA, uPnP, REST API, Android NDK and Design Pattern.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralThanks. Can u give some example for changing background color of a cellmemberBijay Kant Salotry2 Oct '12 - 7:44 
GeneralThanks a lot YajneshmemberSanthapur21 Jul '08 - 6:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 16 Jul 2008
Article Copyright 2008 by Yajnesh Narayan Behera
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid