Posted by: Zeeshan Amjad | November 16, 2009

ListView Control Revisited


We just saw the example of list view control. In that example we saw list view just like a list box. Now let’s explore it little bit more and see the multiple columns in list view and do the data binding with the user define type. Here is a XAML code to display the multiple columns in the list view.

  1: <Window x:Class="UIElement.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     xmlns:local="clr-namespace:UIElement"
  5:     Title="ListView Example" Height="300" Width="400">
  6:     <Grid>        
  7:         <ListView Name="list" Margin="5" HorizontalContentAlignment="Stretch">
  8:             <ListView.View>
  9:                 <GridView>
 10:                     <GridViewColumn Width="Auto" Header="State Name" DisplayMemberBinding="{Binding Path=Name}"/>
 11:                     <GridViewColumn Width="Auto" Header="State Capital" DisplayMemberBinding="{Binding Path=Capital}"/>
 12:                     <GridViewColumn Width="Auto" Header="State Nick Name" DisplayMemberBinding="{Binding Path=NickName}"/>
 13:                 </GridView>
 14:             </ListView.View>
 15:         </ListView>
 16:     </Grid>
 17: </Window>
 18: 

Let’s take a look our old example of State info class. In this C# code we are defining list of state info class and fill it with some data before doing data binding. Here is a complete C# code of this project.

  1: using System;
  2: using System.Collections.Generic;
  3: using System.Linq;
  4: using System.Text;
  5: using System.Windows;
  6: using System.Windows.Controls;
  7: using System.Windows.Data;
  8: using System.Windows.Documents;
  9: using System.Windows.Input;
 10: using System.Windows.Media;
 11: using System.Windows.Media.Imaging;
 12: using System.Windows.Navigation;
 13: using System.Windows.Shapes;
 14: 
 15: namespace UIElement
 16: {
 17:     /// <summary>
 18:     /// Interaction logic for Window1.xaml
 19:     /// </summary>
 20:     public partial class Window1 : Window
 21:     {
 22:         public Window1()
 23:         {            
 24:             InitializeComponent();
 25: 
 26:             List<StateInfo> statesList = new List<StateInfo>();
 27: 
 28:             statesList.Add(new StateInfo("Maryland", "Annapolis", "Old Line State"));
 29:             statesList.Add(new StateInfo("California", "Sacramento", "Golden State"));
 30:             statesList.Add(new StateInfo("Washington", "Olympia", "Ever Green State"));
 31:             statesList.Add(new StateInfo("Taxes", "Austin", "Lone Star State"));
 32:             statesList.Add(new StateInfo("New York", "Albany", "Empire State"));
 33: 
 34:             list.ItemsSource = statesList;
 35:         }
 36:     }
 37: 
 38:     public class StateInfo
 39:     {
 40:         public StateInfo()
 41:         {
 42:         }
 43: 
 44:         public StateInfo(String name, String capital, String nickName)
 45:         {
 46:             Name = name;
 47:             Capital = capital;
 48:             NickName = nickName;
 49:         }
 50: 
 51:         public String Name
 52:         { get; set; }
 53: 
 54:         public String Capital
 55:         { get; set; }
 56: 
 57:         public String NickName
 58:         { get; set; }
 59:     }
 60: }
 61: 

Here is the output of this program.

ListViewOutput_02

Advertisements

Responses

  1. […] and ListView together We have already studied how to use treeview, gridsplitter and list view controls separately. We have information about department and subjects offer by the department […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: