开发者

How to add Items in Winforms comboBox into WPF?

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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜