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

  • 相关阅读:
    【iOS基础控件 6 】 汽车品牌展示 Model嵌套/KVC/TableView索引 <UITableView>
    RAC
    Magical Data Modelling Framework for JSON
    Xcode插件管理工具Alcatraz
    iOS8 Size Classes的理解与使用
    Storyboard、xib中的UIScrollView使用autolayout,使其能够滚动
    iOS事件响应链
    git一些常用的操作(转载)
    iOS本地数据存储(转载)
    使用react-native做一个简单的应用-04界面主框架
  • 原文地址:https://www.cnblogs.com/jayhust/p/5842968.html
Copyright © 2020-2023  润新知