Вопрос

How would I embed a WPF list box to my Windows Form application?

The WPF list box will ideally be able to add list box item objects, render the color (by setting the background property to some color brush), and display the content in the list box from a windows form text box.

I'm not looking to create a composite control in WPF, just embed a simple WPF list box in my Winforms solution.

Thanks!

Это было полезно?

Решение

In your newly create UserControl, you can just expose the ListBox via a property. Then you can access the ListBox via the UserControl.

Somewhere in your UserControl.xaml.cs:

public ListBox MyListBox
{
    get
    {
        return {yourListBoxName};
    }
}

Then when you instantiated your new 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

Alternatively you can have a look at MVVM and bind your ListBox.ListBoxItem to your TextBox;

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top