如何将Winforms组合框中的项目添加到WPF中?
我想将 WinForms ComboBox 添加到我的 WPF 应用程序中。我使用 WindowsFormsHost 添加,但无法将项目添加到 ComboBox。这是我的 XAML 代码:
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
<WindowsFormsHost Name="myWFH">
<wf:ComboBox Name="myCmb" SelectedIndexChanged="ComboBox_SelectedIndexChanged" >
</wf:ComboBox>
</WindowsFormsHost>
public Window2()
{
InitializeComponent();
ComboBox cb = (ComboBox)myWFH.Child; // GIVES ERROR CANNOT CAST
cb.Items.Add("One");
cb.Items.Add("Two");
}
在 XAML 中,我找不到添加项目的方法。在后面的代码中,我无法访问 myCmb,可以访问 myWFH,但不能访问 myCmb。
如何将项目添加到组合框?
I want to add WinForms ComboBox to my WPF application. I added using WindowsFormsHost, but I couldn't add items to the ComboBox. Here is my XAML code :
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
<WindowsFormsHost Name="myWFH">
<wf:ComboBox Name="myCmb" SelectedIndexChanged="ComboBox_SelectedIndexChanged" >
</wf:ComboBox>
</WindowsFormsHost>
public Window2()
{
InitializeComponent();
ComboBox cb = (ComboBox)myWFH.Child; // GIVES ERROR CANNOT CAST
cb.Items.Add("One");
cb.Items.Add("Two");
}
In XAML, I can't find a way to add items. In code behind I can't access myCmb, can access myWFH but not myCmb.
How do I add items to the ComboBox?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您正在尝试将其转换为 WPF ComboBox(System.Windows.Controls.ComboBox)。您应该将其转换为 System.Windows.Forms.ComboBox,然后您就可以添加项目或执行任何您想要的操作。顺便说一句,当您有 WPF ComboBox 时,为什么还要使用表单 ComboBox?
I think that you are trying to cast it to WPF ComboBox(System.Windows.Controls.ComboBox). You should cast it to System.Windows.Forms.ComboBox, and than you can add items or do whatever you want. Btw, why using forms ComboBox when you have WPF ComboBox?