Displaying ListView items - with class! by Rob Bir

Displaying ListView items - with class!  by Rob Birdwell
I don't know about you, but I'm very fond of the ListView control - it's a very useful control to have around. Microsoft's .NET Common Language Runtime (CLR) provides much of the functionality you need to display data in a ListView. However, you're still left will some work to do. Loading the ListView and getting the data can be an arduous task. In fact, your code can get pretty messy unless you think things through a bit. In order to display complex class data, you should consider a way to associate your class data with each ListItem in the ListView.

Let's say you have a class as follows:

class Person                                                                               
   {                                                                                       
   public string Name;                                                                     
   public string Rank;                                                                     
   public string SerialNumber;                                                             
                                                                                           
   public Person (string n, string r, string sn)                                           
      {                                                                                    
      Name = n;                                                                            
      Rank = r;                                                                            
      SerialNumber = sn;  
      }                                                                            
   }

Your form requires you to display the Name, Rank, and SerialNumber members of each Person object in a ListView. We will now create a sub-class of the ListItem base class: "PersonListItem" class. This doesn't involve much code, and the pay-off will be cleaner code and greater flexibility if your display and/or design changes:

// Provide easy access to the Person class within a ListView                              
class PersonListItem : ListItem                                                           
   {                                                                                      
   private Person m_Person;                                                               
                                                                                          
   public PersonListItem (Person person) : base()                                         
      {                                                                                   
      m_Person = Person;                                                                  
                                                                                          
      // Assign Name to the base ListItem Text property -                                 
      // this will cause Name to display by default:                                      
      Text = m_Person.Name;                                                               
                                                                                          
      // Map the other class data to sub-items of the ListItem                            
      // These aren't necessarily displayed...                                            
      SetSubItem(0, m_Person.Rank);                                                        
      SetSubItem(1, m_Person.SerialNumber);                                                
      }                                                                                   
                                                                                          
   // Property for m_Person access                                                        
   public Person PersonData                                                               
      {                                                                                   
      get { return m_Person; }                                                            
      set { m_Person = value; }                                                           
      }                                                                                   
   }                                                                                      

Now all that remains is to add a ListView control to your form called PersonListView. We can optionally add three columns to the ListView: Name, Rank and Serial Number. If you add the columns, be sure to set the View property of PersonListView to "Report." In the form code, we'll add the PersonListItem objects via a simple helper function, called from the form's constructor, as follows:

private void InitPersonListView()                                                         
   {                                                                                      
                                                                                          
   // Load our PersonListView with our important people:                                  
                                                                                          
   PersonListView.InsertItem(PersonListView.ListItems.Count,                              
  new PersonListItem(new Person("Rob Birdwell", "Private", "123456")));                   
                                                                                          
   PersonListView.InsertItem(PersonListView.ListItems.Count,                              
  new PersonListItem(new Person("Bill Gates", "Chairman & CSA", "987654")));              
                                                                                          
   PersonListView.InsertItem(PersonListView.ListItems.Count,                              
   new PersonListItem(new Person("George Bush", "Commander-In Chief", "9999")));          
   }                                                                                      

The benefits of having a PersonListItem sub-class should be obvious - when a user clicks on an item, we have access to all the class data - not just the ListItem.Text property or sub-item data! Here's a sample event handler for our PersonListView's SelectedIndexChanged event:

protected void PersonListView_SelectedIndexChanged (object sender, System.EventArgs e)    
   {                                                                                      
   if (PersonListView.ListItems.Count == 0)                                               
      return; // nothing to do!                                                           
                                                                                          
   // Get the PersonListItem object associated with the selection                         
   PersonListItem li = (PersonListItem)GetCurrentSelectedListItem(PersonListView);        
   Person p = li.PersonData;                                                         
   MessageBox.Show( " Name: " + p.Name +                                                  
                    " Rank: " + p.Rank +                                                  
                    " Serial Number: " p.SerialNumber);                                   
   }                                                                                      
                                                                                          
// Here's a generic helper function for getting a ListView selection:                     
private ListItem GetCurrentSelectedListItem(ListView lv)                                  
   {                                                                                      
                                                                                          
   if (lv.SelectedItems == null || lv.SelectedItems.Count == 0)                           
      return null;                                                                        
                                                                                          
return lv.SelectedItems[0];                                                              
   }                                                                                      

If the PersonListView is defined with no columns, only the Person.Name member will be displayed since it was mapped to the PersonListItem.Text property in the constructor.

And that's what I call displaying ListView items - with class!

时间: 2024-09-19 09:50:18

Displaying ListView items - with class! by Rob Bir的相关文章

Displaying ListView items - with class! by Rob Birdwell

Displaying ListView items - with class!  by Rob Birdwell I don't know about you, but I'm very fond of the ListView control - it's a very useful control to have around. Microsoft's .NET Common Language Runtime (CLR) provides much of the functionality

Displaying ListView items - with class!

I don't know about you, but I'm very fond of the ListView control - it's a very useful control to have around. Microsoft's .NET Framework provides much of the functionality you need to display data in a ListView. However, you're still left will some

xml-如何给 ListView items 创建一个可拉式状态列表?

问题描述 如何给 ListView items 创建一个可拉式状态列表? 在 ListView 的顶部自定义的 ListView selector. 但是我想让 listview 中的文本变成白色,如何实现? <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">

圆弧形ListView(CircleList)

Displaying list items in a semi-circle with a nice fish eye effect. Android. 源代码 : https://github.com/dkmeteor/CircleList 角度/缩放比 可调 angle调至 180度 为半圆形 ------------------- 主要效果为Canvas和Matrix实现的效果,具体看源码就行了.Java代码 public void changeGroupFlag(Object obj)

有数据绑定、排序、查找功能的ListView(二):

排序|数据 using System;using System.Data;using System.Text;using System.Globalization;using System.Collections;using System.Reflection; using System.Drawing;using System.Drawing.Drawing2D;using System.Drawing.Design; using System.Windows.Forms;using Syst

C# Winform ListView虚拟模式

一.前言 虚拟模式在许多情况下都很有用. 如果必须从已存在于内存中的非常大的集合填充一个 ListView 对象,则为每项创建一个 ListViewItem 对象会很浪费. 在虚拟模式下,从缓冲之中获取所需的数据进行加载,性能会有很大提高. 在其他情况下,可能需要经常重新计算 ListViewItem 对象的值,对整个集合进行此操作将产生不可接受的性能. 如下为虚拟模式的一个应用示例(仿VS2010错误列表),截图如下: 二.虚拟模式相关注意点 1.设置一个缓存属性,来保存需要加载的数据:  

Windows 8 Store Apps学习(11) ListView控件和 GridView控件

介绍 重新想象 Windows 8 Store Apps 之 ListView 和 GridView ListView - 列表控件 GridView - 网格控件 示例 1.ListView 的 Demo ListViewDemo.xaml <Page x:Class="XamlDemo.Controls.ListViewDemo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quo

WPF在ListView中填充Mockup数据

用Blend做Visual Design,常常会遇到不知道如何往ListView里放假数据的问 题.这时提供一种方法.供大家参考.如果有更简单的方法,也希望大家不吝赐 教. 代码如下: <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s=&q

“Win10 UAP 开发系列”之 在MVVM模式中控制ListView滚动位置

原文:"Win10 UAP 开发系列"之 在MVVM模式中控制ListView滚动位置 这个扩展属性从WP8.1就开始用了,主要是为了解决MVVM模式中无法直接控制ListView滚动位置的问题.比如在VM中刷新了数据,需要将View中的ListView滚动到顶部,ListView只有一个ScrollIntoView()方法可以控制滚动的位置,但最好在VM中不要出现直接控制View的代码,需要通过其他的方式. 使用一个扩展属性即可实现: /// <summary> ///