Displaying ListView items - with class!
点击次数:31 次 发布日期:2008-11-27 00:35:44 作者:源代码网
|
For example, consider the following class, which represents data you might want to relate to a particular ListView control: 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! 源代码网供稿. |
