Roger
Roger

Reputation: 6527

Can I capture events from a WPF combobox in a winform?

I had implemented a wpf combobox into a winform and now have to somehow make it call a function or an event in my winform when the box is changed, as well as be able to change the value from the winform.

main:

namespace main
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            ElementHost elhost = new ElementHost();
            elhost.Size = new Size(174, 24);
            elhost.Location = new Point(93,60);
            MyWPFControl wpfctl = new MyWPFControl();
            elhost.Child = wpfctl;
            this.Controls.Add(elhost);
            elhost.BringToFront();
        }


        public void status_change(int val)
        {
            MessageBox.Show("Box value has changed to:" + Convert.ToString(val)   );
        }

and wpf:

namespace main
{
    /// <summary>
    /// Interaction logic for MyWPFControl.xaml
    /// </summary>
    public partial class MyWPFControl : UserControl
    {
        public MyWPFControl()
        {
            InitializeComponent();

        }


        private void statComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            main.status_change(statComboBox.SelectedIndex);
            /// says:
            /// Error   1   The type or namespace name 'status_change' does not exist in the namespace 'main' (are you missing an assembly reference?)  C:\Users\Robert\documents\visual studio 2010\Projects\XMPP\main\wpf_combo_status.xaml.cs    31  18  main

        }


    }
    }

any idea what I might be doing wrong? Thanks!

Upvotes: 0

Views: 892

Answers (2)

digEmAll
digEmAll

Reputation: 57210

Expose an event in MyWPFControl like:

public SelectionChangedEventArgs:EventArgs
{
    public int SelectedIndex {get; private set;}
    public SelectionChangedEventArgs (int selIdx)
    {
        this.SelectedIndex = selIdx;
    }
}

public partial class MyWPFControl : UserControl
{
    public event EventHandler<SelectionChangedEventArgs> SelectionChanged;

    public MyWPFControl()
    {
        InitializeComponent();
    }

    private void statComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        OnSelectionChanged(statComboBox.SelectedIndex);
    }

    private void OnSelectionChanged(int selIdx)
    {
        if (SelectionChange != null)
            SelectionChanged(this, new SelectionChangedEventArgs(selIdx));
    }
}

Then subscribe it in your form, like:

public Form1()
{
    InitializeComponent();

    ElementHost elhost = new ElementHost();
    elhost.Size = new Size(174, 24);
    elhost.Location = new Point(93,60);
    MyWPFControl wpfctl = new MyWPFControl();
    elhost.Child = wpfctl;
    this.Controls.Add(elhost);
    elhost.BringToFront();

    wpfctl.SelectionChanged += myWPFControls_SelectionChanged;
}

private void myWPFControls_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    status_change(e.SelectedIndex);
}

P.S.:

I can't test it, so there could be some errors, but just to give you the idea... ;)

Upvotes: 2

madd0
madd0

Reputation: 9323

It's hard to say without more context, but it would seem that the code you want is probably:

private void statComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    main.status_change(statComboBox.SelectedIndex);
}

Assuming main is the instance of Form1 on which you want to call status_change.

Upvotes: 2

Related Questions