I want to add WinForms ComboBox to my WPF application. I added using WindowsFormsHost, but I cou开发者_JAVA百科ldn'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?
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?
精彩评论