wpf和winform(excel中控件)

2022-07-31 12:34:56 浏览数 (5)

大家好,又见面了,我是你们的朋友全栈君。

步骤1:创建WinForm工程

步骤2:在刚刚创建的WinForm工程中新建或者添加现有的WPF用户自定义控件

代码语言:javascript复制
<UserControl x:Class="wndFormTest.ComBoBoxButton"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="55" d:DesignWidth="200">
    <Grid>
        <ComboBox x:Name="_comBox" Foreground="Red" FontSize="24" Margin="0"></ComboBox>   
    </Grid>
</UserControl>
代码语言:javascript复制
public partial class ComBoBoxButton : UserControl
{
    public ComBoBoxButton()
    {
        InitializeComponent();

        // 添加测试数据
        for (int ix = 0; ix < 10; ix  )
            _comBox.Items.Add("abcdefg"   ix.ToString());
    }
}

步骤3:添加相关引用

步骤4:在WinForm面板上添加ElementHost控件(工具箱中) 步骤5:在刚刚的ElementHost中的Child属性中添加刚刚生成的WPF控件(ElementHost是WPF控件的载体)

代码语言:javascript复制
public partial class Form1 : Form
{
    private ElementHost _elemHost = new ElementHost();  // WPF载体
    private ComBoBoxButton _cbb = new ComBoBoxButton(); // WPF控件
    public Form1()
    {
        InitializeComponent();
        
        _elemHost.Location = new Point(50, 50);
        _elemHost.Child = _cbb; // 绑定
        _elemHost.Width = 400;
        _elemHost.Height = 55;

        this.Controls.Add(_elemHost);
    }
}

步骤6:生成解决方案

步骤7:测试结果

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/128623.html原文链接:https://javaforall.cn

0 人点赞