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界面形如以下图例:

  • 相关阅读:
    MFC tab页面中获到其它页面的数据
    sqlite数据库中"Select * From XXX能查到数据,但是Select DISTINCT group From xxx Order By group却查不出来
    关闭程序出现崩溃(exe 已触发了一个断点及未加载ucrtbased.pdb)
    springboot 通用Mapper使用
    springBoot 发布war包
    springCloud Zuul网关
    springboot hystrix turbine 聚合监控
    springBoot Feign Hystrix Dashboard
    springBoot Ribbon Hystrix Dashboard
    springBoot Feign Hystrix
  • 原文地址:https://www.cnblogs.com/jayhust/p/5842968.html
Copyright © 2011-2022 走看看