Smobiler控件的使用:ListView的数据绑定及实现多选
环境
SmobilerDesigner 4.7
Visual Studio 2010以上
正文
listview绑定数据
打开Visual Studio ,新建一个SmobilerApplication项目。从工具箱中找到ListView,CheckBox,Label,Panel等控件拖入到SmobilerFrom1.cs,布局如图
再新建一个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事件)如图
在设计器中打开SmobilerForm1.cs,点击listView1,设置TemplateControlName为SmobilerUserControl1
最后在窗体的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
}
运行效果
彩蛋
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;
}
}
最终效果
Smobiler控件的使用:ListView的数据绑定及实现多选的更多相关文章
- 初始ASP.NET数据控件【续 ListView】
ListView控件 ListView控件可以用来显示数据,它还提供编辑,删除,插入,分页与排序等功能.ListView是GridView与DataList的融合体,它具有GridView控件编辑 ...
- 重新想象 Windows 8.1 Store Apps (93) - 控件增强: GridView, ListView
[源码下载] 重新想象 Windows 8.1 Store Apps (93) - 控件增强: GridView, ListView 作者:webabcd 介绍重新想象 Windows 8.1 Sto ...
- WPF中PasswordBox控件的Password属性的数据绑定
原文:WPF中PasswordBox控件的Password属性的数据绑定 英文原文:http://www.wpftutorial.net/PasswordBox.html 中文原文:http://bl ...
- WPF自定义控件与样式(7)-列表控件DataGrid与ListView自定义样式
一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: Dat ...
- Android控件RecyclerView与ListView的异同
在我的一篇介绍Android新控件RecyclerView的博客(Android L新控件RecyclerView简介)中,一个读者留言说RecyclerView跟ListView之间好像没有什么不同 ...
- 【转】WPF自定义控件与样式(7)-列表控件DataGrid与ListView自定义样式
一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等. 本文主要内容: DataGrid自定义样式: ListView自定义样式: 二.Dat ...
- 【转】WPF中PasswordBox控件的Password属性的数据绑定
英文原文:http://www.wpftutorial.net/PasswordBox.html 中文原文:http://blog.csdn.net/oyi319/article/details/65 ...
- 安卓开发——ListView控件(初始化ListView、列表刷新、长按添加menu)
前言: ListView——列表,它作为一个非常重要的显示方式,不管是在Web中还是移动平台中,都是一个非常好的.不开或缺的展示信息的工具.在Android中,ListView控件接管了这一重担,在大 ...
- Winform开发常用控件之DataGridView的简单数据绑定——自动绑定
DataGridView控件可谓是Winform开发的重点控件,对于数据的呈现和操作非常方便,DataGridView可谓是既简单又复杂.简单在于其已经集成了很多方法,复杂在于可以使用其实现复杂的数据 ...
随机推荐
- NS2网络模拟(5)-homework01.tcl
1: #NS2_有线部分\homework01.tcl 2: 3: #创建两个结点,深圳到北京的TCP连接,图形将数据显示出来,计算吞吐率,画图分析 4: #tcp上层用ftp 5: #udp上层用c ...
- SIIA CODIE AWARDS 2017
Business Technology Best Advertising or Campaign Management Platform Albert, Albert Choozle, Choozle ...
- qt4.8.7 源码在win7+vs2010环境的x64编译(qt 64位)
由于qt官网上,没有直接提供x64的安装包,但由于项目需要x64的qt,所以,小编不得不下载qt的源码,经历了一次长达约4个小时的编译过程.今年国庆7天,就遭这事上了,哈哈~~~ 几个下载链接: qt ...
- Vertica变化Local时间到GMT时间
在Vertica的数据库的使用过程中碰到这么一种场景.程序从不同一时候区的集群中收集数据写入同一张表,然后我们须要把这些数据依照GMT时间来显示. 此时我们能够通过Vertica提供TIME ZONE ...
- Goutte 获取http response
$client = new Goutte\Client(); $crawler = $client->request('GET', 'http://symfony.com'); 获取http 响 ...
- 在IOS开发中使用GoogleMaps SDK
一.申请一个免费的API KEY要使用GoogleMaps SDK,必须要为你的应用申请一个API KEY,API Key可以让你监视你的应用调用api的情况.api key是免费的,你可以在任何调用 ...
- Expression Blend学习四控件
原文:Expression Blend学习四控件 Expression Blend制作自定义按钮 1.从Blend工具箱中添加一个Button,按住shift,将尺寸调整为125*125; 2.右键点 ...
- Qt中使用Boost
编译BOOST库 bjam stage --toolset=qcc --without-graph --without-graph_parallel --without-math --without- ...
- 使用IntelliJ IDEA开发SpringMVC网站(五)博客文章管理
原文:使用IntelliJ IDEA开发SpringMVC网站(五)博客文章管理 摘要 通过对博客文章的管理,实现外键操作. 目录[-] 八.博客文章管理 1.查看文章 2.添加博客 3 ...
- 智能合约开发——以太坊 DApp 实现 购买通证token
合约的buy()方法用于提供购买股票的接口.注意关键字payable,有了它买股票的人才可以付钱给你. 接收钱没有比这个再简单的了! function buy() payable public ret ...