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 ...
随机推荐
- python 8
一.文件操作初识 1. path 文件路径 F:\文件.txt encoding 编码方式 utf-8, gbk ... mode 操作方式 只读,只写,读写,写读,追加... f1 = open(r ...
- CF867E: Buy Low Sell High(贪心, STL) (hdu6438)
Description 有nn个城市,第ii个城市商品价格为aiai,从11城市出发依次经过这nn个城市到达n n城市,在每个城市可以把手头商品出售也可以至多买一个商品,问最大收益. Input 第 ...
- Sails -初级学习配置
新建一个命名为sails的文件夹1.安装 npm -g install sails || cnpm -g install sails 注意:安装必须是全局安装 cnpm install sails - ...
- IntelliJ IDEA 实用快捷键
psvm--------------主(main)-----------------public staitc void main(String[] args) sout--------------- ...
- 5. Web vulnerability scanners (网页漏洞扫描器 20个)
5. Web vulnerability scanners (网页漏洞扫描器 20个) Burp Suite是攻击Web应用程序的集成平台. 它包含各种工具,它们之间有许多接口,旨在方便和加快攻击应用 ...
- Linux第十节课学习笔记
部署LVM三步: 1.pv:使设备支持LVM: 2.vg:对支持LVM的设备进行整合: 3.lv:将整合的空间进行切割. 每个基本单元PE的大小为4M,分配空间必须是4M的整数倍.可以容量或基本单元个 ...
- Educational Codeforces Round 54 (Rated for Div. 2) D:Edge Deletion
题目链接:http://codeforces.com/contest/1076/problem/D 题意:给一个n个点,m条边的无向图.要求保留最多k条边,使得其他点到1点的最短路剩余最多. 思路:当 ...
- centos7镜像文件
1.打开网址https://www.centos.org/download/2.点击Minimall ISO 按钮 3.选择aliyun地址 4.centos版本介绍 在CentOS官方网站上,Ce ...
- Game Development Patterns and Best Practices (John P. Doran / Matt Casanova 著)
https://github.com/PacktPublishing/Game-Development-Patterns-and-Best-Practices https://github.com/m ...
- 注册Docker Hub、以及Push(九)
一.注册 1.使用浏览器打开官网的时候,发现注册按钮点不了 2.下载google访问助手,添加到浏览器 下载地址:http://www.ggfwzs.com/,根据 ...