Posted by: Zeeshan Amjad | November 15, 2009

Using ListView control


ListView control is inherited by ListBox class and it contains all the functionality of List Box class. Here is a class diagram of ListView class.

ListBox

List view define a property View that define the presentation of the List view. WPF already provide one presentation for ListBox class named GridView. If we want to define our own presentation then we have to inherit our class from ViewBase class. ViewBase is an abstract class and View Property of ListView class has the ViewBase data type. Here is a class diagram to explain this.

ListView

Now lets take a look at simple usage of GridView class. Our C# code is very similar to List box example there  we define list of string. Here is C# code of this.

  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 List<String> items
 23:         { get; set; }
 24: 
 25:         public Window1()
 26:         {            
 27:             InitializeComponent();
 28: 
 29:             items = new List<string>();
 30:             items.Add("Maryland");
 31:             items.Add("California");
 32:             items.Add("Taxes");
 33:             items.Add("Washington");
 34:             items.Add("Virginia");
 35: 
 36:             list.ItemsSource = items;
 37:         }
 38:     }
 39: }
 40: 

Here is XAML code of our project.

  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="States Name" DisplayMemberBinding="{Binding}"/>
 11:                 </GridView>
 12:             </ListView.View>
 13:         </ListView>
 14:     </Grid>
 15: </Window>
 16: 

Here is the output of this program.

ListViewOutput

Advertisements

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: