Winform中调用WPF控件

本文介绍两种在WinForms中使用WPF控件的方法。第一种直接在Form中声明并初始化WPF控件,第二种通过创建UserControl并在其XAML文件中定义WPF控件。文章提供代码示例展示如何实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有两种调用方式,在使用之前都将控件“ElementHost”添加到 Form 中:

方式一:

直接声明wpf中控件(使用 Windows.Controls)

Public Class Form1
    Dim txt As Windows.Controls.TextBox = New Windows.Controls.TextBox()
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load        
txt.SpellCheck.IsEnabled = True ElementHost1.Child = txt End Sub End Class

方式二:

首先新建一个 ”User Control“,

之后,如下编辑“UserControl1.xaml”中的内容:

<Grid>
    <TextBox x:Name="textBox" Foreground="Black" FontSize="24" Margin="0"></TextBox>
    <TextBox SpellCheck.IsEnabled="True" />
</Grid>

最后,修改”Form1.vb“代码如下:

Public Class Form1
    Private uc As UserControl1 = New UserControl1()
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ElementHost1.Child = uc
    End Sub
End Class

结果如下所示:

转载于:https://www.cnblogs.com/jizhiqiliao/p/10245390.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值