C# 控件之数据绑定
增加一个委托方法,可以实现后台多线程直接更新UI界面的值,利用了控件的DataBindings,以及 INotifyPropertyChanged接口和事件委托机制。
如果只是通过INotifyPropertyChanged,可在前台单独更新界面,无法通过多线程进行界面值更新。 这可以利用委托和事件的机制,在UI加入事件,通过invoke进行委托调用 ,从而可以实现 。
另外一种方法是设置窗体的CheckForIllegalCrossThreadCalls =false,但该操作方法不是微软建议使用的多线程界面更新的方法。
新建一个winform程序,然后添加两个textbox 和一个button ,写入以下代码 。
using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms; namespace TestFrom
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private TestBind model = new TestBind();
private void Form1_Load(object sender, EventArgs e)
{
// CheckForIllegalCrossThreadCalls = false;
model.NoticeHandler += new PropertyNoticeHandler(PropertyNoticeHandler);
Binding bind1 = new Binding("Text", model, "Value");
Binding bind2 = new Binding("Text", model, "Name");
textBox1.DataBindings.Add(bind1);
textBox2.DataBindings.Add(bind2);
} private void PropertyNoticeHandler(object handle, string proName)
{
try
{
BeginInvoke(
new Action(() => model.Bind(proName)));
}
catch
{
}
} private void button1_Click(object sender, EventArgs e)
{
new Thread(() =>
{
while (true)
{
model.Value++;
model.Name = Guid.NewGuid().ToString(); Thread.Sleep(1000);
}
})
{
IsBackground = true
}.Start();
}
public event PropertyChangedEventHandler PropertyChanged;
} public class TestBind : INotifyPropertyChanged
{
private string name; public string Name
{
get { return name; }
set
{
name = value;
SendChangeInfo("Name");
}
} private int value; public int Value
{
get { return value; }
set
{
this.value = value;
SendChangeInfo("Value");
}
} private void SendChangeInfo(string propertyName)
{
if (NoticeHandler != null)
{
NoticeHandler(this, propertyName);
}
} public void Bind(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
} }
public event PropertyNoticeHandler NoticeHandler;
public event PropertyChangedEventHandler PropertyChanged;
} public delegate void PropertyNoticeHandler(object handle, string proName);
}
C# 控件之数据绑定的更多相关文章
- asp.net学习之 数据绑定控件--List数据绑定控件
原文:asp.net学习之 数据绑定控件--List数据绑定控件 List控件(如 CheckBoxList.DropDownList.ListBox 和 RadioButtonList 类)继承自L ...
- [C#]WinForm 中 comboBox控件之数据绑定
[C#]WinForm 中 comboBox控件之数据绑定 一.IList 现在我们直接创建一个List集合,然后绑定 IList<string> list = new List<s ...
- WPF Label控件在数据绑定Content属性变化触发TargetUpdated事件简单实现类似TextChanged 事件效果
原文:WPF Label控件在数据绑定Content属性变化触发TargetUpdated事件简单实现类似TextChanged 事件效果 本以为Label也有TextChanged 事件,但在使 ...
- WPF 实现跑马灯效果的Label控件,数据绑定方式实现
原文:WPF 实现跑马灯效果的Label控件,数据绑定方式实现 项目中需要使用数据绑定的方式实现跑马灯效果的Label,故重构了Label控件:具体代码如下 using System; using S ...
- WinForm控件复杂数据绑定常用数据源(对Combobox,DataGridView等控件DataSource赋值的多种方法)
开始以前,先认识一下WinForm控件数据绑定的两种形式,简单数据绑定和复杂数据绑定. 1) 简单数据绑定 简单的数据绑定是将用户控件的某一个属性绑定至某一个类型实例上的某一属性.采用如下形式进行绑定 ...
- WP8.1学习系列(第二十三章)——到控件的数据绑定
在本文中 先决条件 将控件绑定到单个项目 将控件绑定到对象的集合 通过使用数据模板显示控件中的项目 添加详细信息视图 转换数据以在控件中显示 相关主题 本主题介绍了如何在使用 C++.C# 或 Vis ...
- Windows App开发之集合控件与数据绑定
为ListView和GridView加入数据 ListView採用垂直堆叠得方式显示数据.而GridView则採用水平堆叠得方式. 长相的话嘛,它们都几乎相同. <Grid Name=" ...
- 《ASP.NET1200例》嵌套在DataLisT控件中的其他服务器控件---DropDownList控件的数据绑定
aspx <script type="text/javascript"> function CheckAll(Obj) { var AllObj = document. ...
- Repeater控件实现数据绑定,并实现分页效果
前台显示代码 <pre name="code" class="csharp"><asp:Repeater ID="Repeater1 ...
随机推荐
- spring boot 项目部署在阿里云上
装jdk, 然后 nohup java -jar xxxx.jar> system.log 2>&1 & lsof -i:9999 kill -9 1234
- penn treebank的说明,包括很多语法知识
penn数据库说明 http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.9.8216&rep=rep1&type=pdf
- HTML下标签之应用
<!doctype html><html lang="en"> <head> <meta charset="UTF-8" ...
- bee: command not found问题解决之道
$ bee bash: bee: command not found 遇到这个错误的时候,我希望您是所有环境全部安装好的情况下遇到的,如果你的环境没有安装好请参考 beego环境搭建http://bl ...
- myEclipse出现cannot paste the clipboard contents into the selected elements报错
导入jar包报错,cannot paste the clipboard contents into the selected elements,查阅资料让重新打开工程,但依然报错. 最后在本地路径复制 ...
- 网络操作基础(two)
P106 一.什么是活动目录?活动目录有哪些优点? 二.什么是域.域树.森林? 三.什么是信任?什么是域的方向及传递性? 四.如何管理活动目录的信任与站点? 解答! (一) 1.活动目录:提供了用于存 ...
- NodeJS 学习笔记
1. NodeJs的事件模型被称为非阻塞式IO或者事件驱动IO 2. Node.js 几乎每一个 API 都是支持回调函数的. 3. Node.js 基本上所有的事件机制都是用设计模式中观察者模式实现 ...
- [JAVA]JAVA实现多线程的三种方式
1.继承Thread类,通过start()方法调用 public class MultiThreadByExtends extends Thread { @Override public void r ...
- 使用Visual Studio给SQL生成测试数据
参考:http://www.cnblogs.com/CareySon/archive/2012/02/20/2359444.html 使用VS2010的数据生成计划来生成测试数据 以下面两个表来做例子 ...
- osx免驱网卡推荐
1. 单频2.4G芯片为Realtek RTL8188cu, RTL8192cu,都可以用,如TP-Link TL-WN821N.TP-Link TL-WN823N等等:2. 单频2.4G芯片为Med ...