環(huán)境
SmobilerDesigner 4.7
Visual Studio 2010以上
正文
listview綁定數(shù)據(jù)
打開Visual Studio ,新建一個SmobilerApplication項(xiàng)目。從工具箱中找到ListView,CheckBox,Label,Panel等控件拖入到SmobilerFrom1.cs,布局如圖?
再新建一個SmobilerUserControl類,暫且命名為SmobilerUserControl1.cs,設(shè)置Size為(300,50),再拖入CheckBox,Label,Panel控件,CheckBox的DataMember設(shè)為id,Modifiers設(shè)為public,label的DisplayMember設(shè)為lab,Modifiers設(shè)為public,panel的Touchable設(shè)為true,(panel的Touchable設(shè)為true時點(diǎn)擊可以觸發(fā)press事件)如圖?
在設(shè)計器中打開SmobilerForm1.cs,點(diǎn)擊listView1,設(shè)置TemplateControlName為SmobilerUserControl1
最后在窗體的load事件中綁定數(shù)據(jù)源
private void SmobilerForm1_Load(object sender, EventArgs e)
???????? {
???????????? DataTable dt = new DataTable();
???????????? dt.Columns.Add("id");
???????????? dt.Columns.Add("lab");
???????????? for (int i = 0; i < 5; i++)
???????????????? dt.Rows.Add(i, "圖書" + i.ToString());
???????????? listView1.DataSource = dt;
???????????? listView1.DataBind();
???????? }
注:控件的DataMember和DisplayMember有什么不同呢?DataMember是值綁定字段,DisplayMember是顯示綁定字段,簡單來說就是DisplayMember綁定的值會顯示,DataMember則不顯示,本例中的label的displayMember綁定后值是賦給了Text屬性;checkbox的DataMember綁定后在界面上顯示,需要通過checkbox.BindDataValue來獲取。
實(shí)現(xiàn)全選
思路:借助list來存儲勾選項(xiàng),listview中的行項(xiàng)每勾選一個就往list插入一條記錄,取消勾選則從list中移除記錄,當(dāng)list.Count與listview的行數(shù)相同是則表示全部選擇
1.SmobilerForm1.cs中代碼
List
///
???????? /// 添加勾選項(xiàng)
???????? ///
???????? ///
???????? public void AddSelectItem(string item)
???????? {
???????????? if (!selectItem.Contains(item))
???????????????? selectItem.Add(item);
???????? }
???????? ///
???????? /// 取消選擇
???????? ///
???????? ///
???????? public void RemoveSelectItem(string item)
???????? {
???????????? if (selectItem.Contains(item))
???????????????? selectItem.Remove(item);
???????? }
???????? ///
???????? /// 改變checkbox狀態(tài)
???????? ///
???????? public void changeState()
???????? {
???????????? if (selectItem.Count == listView1.Rows.Count)//selectItem的數(shù)量和listview.Rows的數(shù)量一致表示全選
???????????????? checkBox1.Checked = true;
???????????? else
???????????????? checkBox1.Checked = false;
???????? }
???????? ///
???????? /// checkbox點(diǎn)擊事件
???????? ///
???????? ///
???????? ///
???????? private void checkBox1_CheckedChanged(object sender, EventArgs e)
???????? {
???????????? if (checkBox1.Checked)
???????????? {
???????????????? foreach (ListViewRow row in listView1.Rows)//遍歷listview.Rows
???????????????? {
???????????????????? //(SmobilerUserControl1)row.Control即listview行模板
???????????????????? ((SmobilerUserControl1)row.Control).checkBox1.Checked = true;//改變listview模板里中的checkbox值
???????????????????? AddSelectItem(((SmobilerUserControl1)row.Control).checkBox1.BindDataValue.ToString());//獲取模板類中的checkbox的DataMember
???????????????? }
}
???????????? else
???????????? {
???????????????? foreach (ListViewRow row in listView1.Rows)
???????????????? {
???????????????????? ((SmobilerUserControl1)row.Control).checkBox1.Checked = false;
???????????????????? RemoveSelectItem(((SmobilerUserControl1)row.Control).checkBox1.BindDataValue.ToString());
???????????????? }
???????????? }
2.SmobilerUserControl1.cs中,模板類使用(SmobilerForm1)this.Form來調(diào)用SmobilerForm1的屬性、方法,將數(shù)據(jù)傳給SMobilerForm1
private void checkBox1_CheckedChanged(object sender, EventArgs e)
???????? {
???????????? SmobilerForm1 frm = (SmobilerForm1)this.Form;//獲取listview所在窗體
???????????? if (checkBox1.Checked)
???????????? {
???????????????? frm.AddSelectItem(checkBox1.BindDataValue.ToString());
???????????? }
???????????? else
???????????? {
???????????????? frm.RemoveSelectItem(checkBox1.BindDataValue.ToString());
???????????? }
frm.changeState();
???????? }
???????? ///
???????? /// panel點(diǎn)擊事件
???????? ///
???????? ///
???????? ///
private void panel1_Press(object sender, EventArgs e)
???????? {
???????????? SmobilerForm1 frm = (SmobilerForm1)this.Form;
???????????? frm.label3.Text = label1.Text;//數(shù)據(jù)傳給SMobilerForm1
???????? }
運(yùn)行效果
彩蛋
listview實(shí)現(xiàn)單雙行間隔色
在4.7版本中,可以在ListView的RowBind事件中,通過設(shè)置 e.Row.Control.BackColor來設(shè)置不同的顏色,RowBind事件是在行綁定后發(fā)生,具體見(https://www.smobiler.com/Help/html/E_Smobiler_Core_Controls_ListView_RowBind.htm),代碼如下
bool flag = true;//通過flag判斷單雙行
???????? private void listView1_RowBind(object sender, ListViewTemplateBindEventArgs e)
???????? {
???????????? if (flag)
???????????? {
???????????????? e.Row.Control.BackColor = System.Drawing.Color.White;//第0行開始,偶數(shù)白色單數(shù)藍(lán)色
???????????????? flag = !flag;
???????????? }
???????????? else
???????????? {
???????????????? e.Row.Control.BackColor = System.Drawing.Color.SkyBlue;
???????????????? flag = !flag;
???????????? }
???????? }
最終效果
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。