码迷,mamicode.com
首页 > Windows程序 > 详细

C# WinForm中使用WPF的控件

时间:2015-04-23 09:37:58      阅读:350      评论:0      收藏:0      [点我收藏+]

标签:wpf   winform   控件   

步骤1:创建WinForm工程

技术分享


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

技术分享

<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>
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控件的载体)

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:测试结果

技术分享

C# WinForm中使用WPF的控件

标签:wpf   winform   控件   

原文地址:http://blog.csdn.net/aoshilang2249/article/details/45216239

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!