zoukankan      html  css  js  c++  java
  • 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界面形如以下图例:

  • 相关阅读:
    群辉:服务器错误 错误代码38
    wireshark filter manualpage
    收集下shell使用笔记
    Android kernel LOGO的更换方法
    java实现截屏
    [转]android4.0.3 修改启动动画和开机声音
    博客搬迁
    idea 2017.3创建springboot项目报无效的源发行版: 1.8或者Unsupported major.minor version 52.0的解决方案
    关于mybatis查询集合返回为[null]的问题
    关于mybatis中resultType返回null的问题
  • 原文地址:https://www.cnblogs.com/jayhust/p/5842968.html
Copyright © 2011-2022 走看看