控制WPF在Winforms内不刷新
本文关键字:刷新 Winforms WPF 控制 | 更新日期: 2023-09-27 18:02:40
我很抱歉我的英语。
我有一个WINFORMS项目,其中有一个WPF用户控件。
WPF正确地显示了我,但是当我更新WPF中的一些控件时,没有更新我这样的控件。(文本框,标签,图像等)
在Winforms中留下一个简单的WPF示例,当您单击该按钮时,应该在文本框中显示"Hello World"。但我没有工作。
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace IntegracionWPF
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// Create the ElementHost control for hosting the
// WPF UserControl.
ElementHost host = new ElementHost();
host.Dock = DockStyle.Fill;
// Create the WPF UserControl.
UserControl1 userControl1 = new UserControl1();
userControl1.setText("Hola Mundo");
// Assign the WPF UserControl to the ElementHost control's
// Child property.
host.Child = userControl1;
// Add the ElementHost control to the form's
// collection of child controls.
this.Controls.Add(host);
}
}
}
userControl1.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace IntegracionWPF
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public void setText(string text)
{
textBox1.Text = text;
}
}
}
希望您正在寻找元素宿主。这对你有帮助。修改按钮,点击form1
private void button1_Click(object sender, EventArgs e)
{
// Create the ElementHost control for hosting the
// WPF UserControl.
ElementHost host = new ElementHost();
host.Dock = DockStyle.Fill;
// Create the WPF UserControl.
UserControl1 userControl1 = new UserControl1();
userControl1.setText("Hola Mundo");
// Assign the WPF UserControl to the ElementHost control's
// Child property.
host.Child = userControl1;
// Add the ElementHost control to the form's
// collection of child controls.
this.Controls.Add(host);
}