博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WINFORM中加入WPF控件并绑定数据源实现跨线程自动更新
阅读量:4312 次
发布时间:2019-06-06

本文共 3148 字,大约阅读时间需要 10 分钟。

1. WINFORM中添加两个ElementHost,一个放WPF的Button,一个放WPF的TextBox。其中TextBox与数据源绑定,实现跨线程也可以自动更新,而不会出现WINFORM的TextBox控件与数据源绑定后,存在子线程中更新数据源报错(跨线程更新控件)的情况。

using System;using System.Collections.Generic;using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows; // 自行添加支持WPF控件using System.Windows.Data; // 自行添加支持WPF控件using System.Windows.Controls; // 自行添加支持WPF控件using System.Windows.Forms;using System.IO.Ports;using System.Net;using System.Net.Sockets;using System.Threading;namespace CrossThreadBindingTest{    public partial class Form1 : Form    {        public class DataSource : INotifyPropertyChanged        {            private int _index;            public int Index            {                get { return _index; }                set                {                    _index = value;                    if (PropertyChanged != null)                    {                        this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Index"));                    }                }            }            public event PropertyChangedEventHandler PropertyChanged;        }                System.Windows.Data.Binding _bind;        Thread _thread;        DataSource _dataSource;        bool _run;                public Form1()        {            InitializeComponent();            // Create a WPF Button            System.Windows.Controls.Button btn = new System.Windows.Controls.Button();            btn.Content = "Button in WPF"; // 修改内容属性            System.Windows.Media.FontFamily font = new System.Windows.Media.FontFamily("Ariel"); // 修改字体属性            btn.FontFamily = font;            //btn.Click += new System.Windows.RoutedEventHandler(btn_Click); // 增加事件响应            // Add it to ElementHost            elementHost1.Child = btn;            // Create a WPF TextBox            System.Windows.Controls.TextBox txtBox = new System.Windows.Controls.TextBox();            txtBox.Text = "TextBox in WPF"; // 修改内容属性            txtBox.FontFamily = font;            _dataSource = new DataSource();            // System.Windows.Data.Binding方式            _bind = new System.Windows.Data.Binding();            _bind.Source = _dataSource;            _bind.Path = new PropertyPath("Index");            _bind.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;            txtBox.SetBinding(System.Windows.Controls.TextBox.TextProperty, _bind); // 增加数据源绑定            // Add it to ElementHost            elementHost2.Child = txtBox;            _run = true;            _thread = new Thread(Test);            _thread.Start();        }        void Test()        {            while (_run)            {                _dataSource.Index++;                Thread.Sleep(100);            }         }        private void Form1_FormClosing(object sender, FormClosingEventArgs e)        {            _run = false;            if (_thread != null)            {                _thread.Join();                _thread = null;            }        }    }}

 

posted on
2018-01-29 13:54 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/jayhust/p/8376964.html

你可能感兴趣的文章
oracle存储过程杂记
查看>>
JPA @Id 和 @GeneratedValue 注解详解
查看>>
自定义Template
查看>>
su 和 su -
查看>>
MapReduce:共同好友详解
查看>>
python函数:匿名函数、函数递归与二分法、面向过程编程
查看>>
final、static、代码块、静态代码块、内部类、代码执行顺序
查看>>
LeetCode-Burst Balloons
查看>>
LeetCode-Bitwise AND of Numbers Range
查看>>
Windows Server 2012和2008中使用计划任务定时执行BAT批处理文件 定时备份mysql数据...
查看>>
费马小定理与GCD&LCM
查看>>
P1077 摆花
查看>>
zynq修改ramdisk文件系统
查看>>
C#测量程序运行时间及cpu使用时间
查看>>
并发编程
查看>>
我自己曾经经历的CMMI3认证通过关于软件测试的访谈【转载】
查看>>
C# 操作Excel ——Excel获取数据、时间、图片
查看>>
【Express系列】第3篇——接入mysql
查看>>
js 高亮显示关键字
查看>>
CPU工作原理简图
查看>>