WINFORM中加入WPF控件并绑定数据源实现跨线程自动更新

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;
            }
        }
    }
}
原文地址:https://www.cnblogs.com/jayhust/p/8376964.html