Smobiler控件的使用:ListView的數據綁定及實現多選

環境
SmobilerDesigner 4.7
Visual Studio 2010以上
html


正文工具

listview綁定數據佈局

打開Visual Studio ,新建一個SmobilerApplication項目。從工具箱中找到ListView,CheckBox,Label,Panel等控件拖入到SmobilerFrom1.cs,佈局如圖  this

image

再新建一個SmobilerUserControl類,暫且命名爲SmobilerUserControl1.cs,設置Size爲(300,50),再拖入CheckBox,Label,Panel控件,CheckBox的DataMember設爲id,Modifiers設爲public,label的DisplayMember設爲lab,Modifiers設爲public,panel的Touchable設爲true,(panel的Touchable設爲true時點擊能夠觸發press事件)如圖  設計

image

在設計器中打開SmobilerForm1.cs,點擊listView1,設置TemplateControlName爲SmobilerUserControl13d

image

最後在窗體的load事件中綁定數據源orm

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();
         }htm

注:控件的DataMember和DisplayMember有什麼不一樣呢?DataMember是值綁定字段,DisplayMember是顯示綁定字段,簡單來講就是DisplayMember綁定的值會顯示,DataMember則不顯示,本例中的label的displayMember綁定後值是賦給了Text屬性;checkbox的DataMember綁定後在界面上顯示,須要經過checkbox.BindDataValue來獲取。blog


實現全選事件

思路:藉助list來存儲勾選項,listview中的行項每勾選一個就往list插入一條記錄,取消勾選則從list中移除記錄,當list.Count與listview的行數相同是則表示所有選擇

1.SmobilerForm1.cs中代碼

List<string> selectItem = new List<string>();//經過這個list來記錄已勾選的數據行id

          /// <summary>
         /// 添加勾選項
         /// </summary>
         /// <param name="item"></param>
         public void AddSelectItem(string item)
         {
             if (!selectItem.Contains(item))
                 selectItem.Add(item);
         }
         /// <summary>
         /// 取消選擇
         /// </summary>
         /// <param name="item"></param>
         public void RemoveSelectItem(string item)
         {
             if (selectItem.Contains(item))
                 selectItem.Remove(item);
         }
         /// <summary>
         /// 改變checkbox狀態
         /// </summary>
         public void changeState()
         {
             if (selectItem.Count == listView1.Rows.Count)//selectItem的數量和listview.Rows的數量一致表示全選
                 checkBox1.Checked = true;
             else
                 checkBox1.Checked = false;
         }
         /// <summary>
         /// checkbox點擊事件
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         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來調用SmobilerForm1的屬性、方法,將數據傳給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();
         }
         /// <summary>
         /// panel點擊事件
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
          private void panel1_Press(object sender, EventArgs e)
         {
             SmobilerForm1 frm = (SmobilerForm1)this.Form;
             frm.label3.Text = label1.Text;//數據傳給SMobilerForm1
         }
運行效果

lula201902_4



彩蛋

listview實現單雙行間隔色

在4.7版本中,能夠在ListView的RowBind事件中,經過設置 e.Row.Control.BackColor來設置不一樣的顏色,RowBind事件是在行綁定後發生,具體見(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行開始,偶數白色單數藍色
                 flag = !flag;
             }
             else
             {
                 e.Row.Control.BackColor = System.Drawing.Color.SkyBlue;
                 flag = !flag;
             }
         }
最終效果


lula201902_5
相關文章
相關標籤/搜索