WINFORM中加入WPF控件並綁定數據源實現跨線程自動更新


 

1. WINFORM中添加兩個ElementHost,一個放WPF的Button,一個放WPF的TextBox。其中TextBox與數據源綁定,實現跨線程也可以自動更新,而不會出現WINFORM的TextBox控件與數據源綁定后,存在子線程中更新數據源報錯(跨線程更新控件)的情況。

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 UI
{
    public partial class MainForm : 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 MainForm()
        {
            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 btn_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.MessageBox.Show("WPF button clicked!");
        }
        
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            _run = false;
            if (_thread != null)
            {
                _thread.Join();
                _thread = null;
            }
        }
    }
}

 WINFORM界面形如以下圖例:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM