Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将WPF列表框嵌入到我的WinForms应用程序_C#_.net_Wpf_Listbox_Winforms Interop - Fatal编程技术网

C# 将WPF列表框嵌入到我的WinForms应用程序

C# 将WPF列表框嵌入到我的WinForms应用程序,c#,.net,wpf,listbox,winforms-interop,C#,.net,Wpf,Listbox,Winforms Interop,如何将WPF列表框嵌入Windows窗体应用程序 WPF列表框在理想情况下能够添加列表框项对象、渲染颜色(通过将“背景”属性设置为某个颜色笔刷),以及从windows窗体文本框显示列表框中的内容 我不想在WPF中创建复合控件,只想在我的Winforms解决方案中嵌入一个简单的WPF列表框 谢谢 在新创建的UserControl中,您可以通过属性公开列表框。然后,您可以通过用户控件访问列表框 在UserControl.xaml.cs中的某个位置: public ListBox MyListBox

如何将WPF列表框嵌入Windows窗体应用程序

WPF列表框在理想情况下能够添加列表框项对象、渲染颜色(通过将“背景”属性设置为某个颜色笔刷),以及从windows窗体文本框显示列表框中的内容

我不想在WPF中创建复合控件,只想在我的Winforms解决方案中嵌入一个简单的WPF列表框


谢谢

在新创建的
UserControl
中,您可以通过属性公开
列表框。然后,您可以通过
用户控件
访问
列表框

在UserControl.xaml.cs中的某个位置:

public ListBox MyListBox
{
    get
    {
        return {yourListBoxName};
    }
}
然后,当您实例化新的
UserControl
时:

ElementHost elhost = new ElementHost();
elhost.Size = new Size(110, 60);
elhost.Location = new Point(45,35);

MyWPFControl wpfctl = new MyWPFControl();
elhost.Child = wpfctl;

this.Controls.Add(elhost);

//Access your ListBox via wpfctl.MyListBox

或者,您可以查看并将
列表框.ListBoxItem
绑定到
文本框

看看Thank@Willem,它在大多数情况下都有效,但是,我不确定如何访问我程序中嵌入的列表框。。。。。。。。。。。当ListBoxItem被发送到Winforms文本框时,我需要创建一些事件处理程序来接收对它的请求吗?谢谢@Willem,使用这个方法,我现在可以在我的用户控件中公开ListBox属性了。问题:我们返回的是您的“yourListBoxName”是什么?这就是我得到的:公共部分类UserControl1:UserControl{//public string yourListBoxName;public ListBox theListBox=new ListBox();public UserControl1(){InitializeComponent();}public ListBox MyListBox{get{return theListBox;}}}
@discofighter411将
ListBox
添加到
WPF
时,给
ListBox
一个“名称”。然后在返回属性时使用该名称。@Willem-谢谢,修复后效果很好!