ObjectListView控件介紹及C# Demo實現


1 介紹

前兩天在用Listview做數據顯示時,希望在listview中的多列顯示圖片,經過一番搜索后發現.Net自帶的ListView其實只能在各行的第一列顯示圖片。后來google時看到網友有提及ObjectListView這個開源控件,一查發現確實很強大,於是決定秉承拿來主義的思想,學習使用之。

2 背景    

ObjectListView使用C#語言對.Net框架下的ListView進行了封裝,使ListView的用法更簡單、顯示內容更豐富完美。下圖是一個簡單的ObjectListView的例子:

貼圖的效果是暴力的、強大的,無需多言,與.Net原生的ListView相比,高下立見。

ObjectListView的官網:http://objectlistview.sourceforge.net/cs/index.html .

3 代碼示例

相信,對於大多數處於搬運工階段的Coder來說(很慚愧,鄙人還處於這階段),能用的代碼Demo才是王道!

Step 1: ObjectListView庫的引入。有加工程依賴及DLL引用兩種方式,這里選擇引入DLL的方式。

    在官網下載ObjectListView.dll,然后新建WinForm工程,在Reference->Add Reference->選擇剛下載的DLL。然后需要注意的一個操作是需要更改當前工程的項目屬性: 右鍵你所見的工程,選擇屬性:

    圖中的Target framework開始默認是.Net Framework 4 Client Profile, 將其更改為.Net Framework 4. 否則在將ObjectListView控件放至Form上編譯時會報如下錯誤:

Step 2: ObjectListView控件的使用。

   1) 在工具欄顯示ObjectListView控件:

    在Toolbox的General Tab下面郵件選擇 Choose Items… -> .Net Framework Components選項卡中,選擇Browser選擇剛加入的ObjectListView.dll,確定后在 General選項卡下面或All Windows Forms下即可看見objectlistview等幾個控件。

  2) 添加用戶數據顯示的類Car.cs.  其內容如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyObjectListviewDemo
{
    class Car
    {
        private string channel;
        private string hasCar;
        private string node;

        //Public Properties to show in the objectlistview columns.
        public string Channel { get { return channel; } set { channel = value; } }
        public string HasCar { get { return hasCar; } set { hasCar = value; } }
        public string Node { get { return node; } set { node = value; } }

        public Car(string channel, string hasCar, string node) {
            this.channel = channel;
            this.hasCar = hasCar;
            this.node = node;
        }

        public static List<Car> carList = new List<Car>();

        public static List<Car> GetCarList() {
            if (carList.Count == 0)
                carList = Car.initCarList();
            return carList;
        }

        private static List<Car> initCarList() {
            List<Car> list = new List<Car>();
            list.Add(new Car("1", "no", "001"));
            list.Add(new Car("2", "yes", "002"));
            list.Add(new Car("3", "yes", "003"));
            return list;
        }
    }
}
View Code

  3) 在Form上拖入一個ObjectListView控件、一個imageList控件:    如圖:點擊ObjectListview右上角的三角形,將其View設置Details,Samll ImageList設置為加入的imageList1空間。

    然后單擊Edit Columns…編輯列:點擊Add加入三個Column,在右邊的屬性設置中,將Text設置為列頭顯示的文字,將AspectName設置為Car類中對應的Public屬性的值。

    設置要顯示的可選擇的圖片,設置imagelist的collection.    在Form上右擊imageList空間進入屬性設置,點擊Images: Collection添加你要顯示的圖片,如圖。

 

  4)   在Form.cs中進行數據綁定:其具體代碼如下:

 

     
 private void Form1_Load(object sender, EventArgs e)
        {
            this.olvColumn2.ImageGetter = new BrightIdeasSoftware.ImageGetterDelegate(this.ShowCarImage);
            this.objectListView1.SetObjects(Car.GetCarList());
        }

        private object ShowCarImage(object rowObject) {
            Car car = (Car)rowObject;
            if (car.HasCar == "yes")
            {
                return "hascar.png";
            }
            else {
                return "nocar.png";
             }
        }
View Code

  5) 編譯運行得以下結果:

  可見,在第二行已經能夠顯示圖片了,本篇目的已達到,收工。

本Demo完整示例代碼下載:MyObjectListView.zip

4 討論

    本例中,只使用了最基本的ObjectListView控件屬性,主要從本人的實際需要出發而寫的Demo,更多地介紹請參見ObjectListView官網。

5 版權

在注明出處的情況下,可自由進行復制傳播。

轉載請注明出處:http://www.cnblogs.com/Rayblog/p/4542688.html

6 關於作者

Ray Lei,正處於積累學習階段的.Net平台開發者,同時研究關注移動開發技術。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM