增加一个委托方法,可以实现后台多线程直接更新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# 控件之数据绑定的更多相关文章

  1. asp.net学习之 数据绑定控件--List数据绑定控件

    原文:asp.net学习之 数据绑定控件--List数据绑定控件 List控件(如 CheckBoxList.DropDownList.ListBox 和 RadioButtonList 类)继承自L ...

  2. [C#]WinForm 中 comboBox控件之数据绑定

    [C#]WinForm 中 comboBox控件之数据绑定 一.IList 现在我们直接创建一个List集合,然后绑定 IList<string> list = new List<s ...

  3. WPF Label控件在数据绑定Content属性变化触发TargetUpdated事件简单实现类似TextChanged 事件效果

    原文:WPF Label控件在数据绑定Content属性变化触发TargetUpdated事件简单实现类似TextChanged 事件效果   本以为Label也有TextChanged 事件,但在使 ...

  4. WPF 实现跑马灯效果的Label控件,数据绑定方式实现

    原文:WPF 实现跑马灯效果的Label控件,数据绑定方式实现 项目中需要使用数据绑定的方式实现跑马灯效果的Label,故重构了Label控件:具体代码如下 using System; using S ...

  5. WinForm控件复杂数据绑定常用数据源(对Combobox,DataGridView等控件DataSource赋值的多种方法)

    开始以前,先认识一下WinForm控件数据绑定的两种形式,简单数据绑定和复杂数据绑定. 1) 简单数据绑定 简单的数据绑定是将用户控件的某一个属性绑定至某一个类型实例上的某一属性.采用如下形式进行绑定 ...

  6. WP8.1学习系列(第二十三章)——到控件的数据绑定

    在本文中 先决条件 将控件绑定到单个项目 将控件绑定到对象的集合 通过使用数据模板显示控件中的项目 添加详细信息视图 转换数据以在控件中显示 相关主题 本主题介绍了如何在使用 C++.C# 或 Vis ...

  7. Windows App开发之集合控件与数据绑定

    为ListView和GridView加入数据 ListView採用垂直堆叠得方式显示数据.而GridView则採用水平堆叠得方式. 长相的话嘛,它们都几乎相同. <Grid Name=" ...

  8. 《ASP.NET1200例》嵌套在DataLisT控件中的其他服务器控件---DropDownList控件的数据绑定

    aspx <script type="text/javascript"> function CheckAll(Obj) { var AllObj = document. ...

  9. Repeater控件实现数据绑定,并实现分页效果

    前台显示代码 <pre name="code" class="csharp"><asp:Repeater ID="Repeater1 ...

随机推荐

  1. Python全栈之路----目录

    Module1 Python基本语法 Python全栈之路----编程基本情况介绍 Python全栈之路----常用数据类型--集合 Module2 数据类型.字符编码.文件操作 Python全栈之路 ...

  2. 解决Ubuntu19.04下网易云音乐打不开的问题

    Ubuntu19.04下打开网易云音乐的v18.04版会出现以下错误: opt/netease/netease-cloud-music/netease-cloud-music: symbol look ...

  3. 解决Maven web 项目 Cannot detect Web Project version. Please specify version of Web Project through ... 的错误

    创建maven项目的时候,maven文件头报错: Cannot detect Web Project version. Please specify version of Web Project th ...

  4. java 删除多层文件夹

    /** * 因为不小心,写了一个死循环,在电脑里创建的了n多层空文件夹 * 并且手动最外层删除不掉. * 所以用写了本代码,从里向外的进行删除操作. * @author Singularity * @ ...

  5. 熟悉 JUnit 测试

    2.1 Mooctest 使用心得 web Ide挺方便,就是很慢.mooctest很方便入门软件测试,但是里面的题目还是不多. 2.2 Junit 编写代码经验总结 1.首先要熟悉junit中经常使 ...

  6. linux 下 mac 地址如何查询

    cat     /sys/class/net/eth0/address

  7. JavaScript作用域(第七天)

    我们都知道js代码是由自上而下的执行,但我们来看看下面的代码: test(); function test(){ console.log("hello world"); }; 如果 ...

  8. 学习笔记TF054:TFLearn、Keras

    元框架(metaframework). TFLearn.模块化深度学习框架,更高级API,快速实验,完全透明兼容. TFLearn实现AlexNet.https://github.com/tflear ...

  9. 《我的嵌入式开发》---- IIC 通信

    IIC 通用文件,文件是在NRF51xx 芯片基础,keil 平台开发测试通过,后期修改为STM32F2xx系列的配置. 文件百度云盘链接 : https://pan.baidu.com/s/1AFx ...

  10. Maven多项目继承:dependencyManagement scope=import

    maven的多项目结构中,可以使用parent定义起父项目,从而从父项目中继承依赖等属性.但是美中不足,maven只能单继承,即一个项目只能使用parent标签定一个父级项目. maven2.9之后的 ...