Smobiler控件的使用:ListView的数据绑定及实现多选

环境
SmobilerDesigner 4.7
Visual Studio 2010以上


正文

listview绑定数据

打开Visual Studio ,新建一个SmobilerApplication项目。从工具箱中找到ListView,CheckBox,Label,Panel等控件拖入到SmobilerFrom1.cs,布局如图 

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为SmobilerUserControl1

image

最后在窗体的load事件中绑定数据源

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来获取。


实现全选

思路:借助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

猜你喜欢

转载自blog.51cto.com/14360220/2411425