Use Smobiler controls: ListView data binding and the realization of multiple-choice

Environmental
SmobilerDesigner 4.7
Visual Studio 2010 or more


text

listview bound data

Open Visual Studio, create a new SmobilerApplication project. Find ListView, CheckBox, Label, Panel and other controls from the toolbox to drag SmobilerFrom1.cs, as shown in the layout 

image

Then create a SmobilerUserControl class, for the time being named SmobilerUserControl1.cs, set the Size to (300, 50), and then drag the CheckBox, Label, Panel control, CheckBox's DataMember set id, Modifiers is set to public, label DisplayMember set of lab , Modifiers is set to public, panel of Touchable set to true, (panel of Touchable click when set to true events can trigger press) Figure 

image

Open SmobilerForm1.cs in the designer, click listView1, set TemplateControlName to SmobilerUserControl1

image

Finally, the bound data source in the load event of the form

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; // odd and even lines by determining flag
         Private void listView1_RowBind (Object SENDER, ListViewTemplateBindEventArgs E)
         {
             IF (flag)
             {
                 e.Row.Control.BackColor = System.Drawing.Color.White; // row 0 starts, the even-odd blue white
                 In Flag In Flag =;!
             }
             the else
             {
                 e.Row.Control.BackColor = System.Drawing.Color.SkyBlue;
                 In Flag In Flag =;!
             }
         }
final results


lula201902_5

Guess you like

Origin blog.51cto.com/14360220/2411425