我可以从 winform 中的 WPF 组合框捕获事件吗?

发布于 2024-10-21 20:47:16 字数 1523 浏览 1 评论 0原文

我已经在 winform 中实现了一个 wpf 组合框,现在必须以某种方式使其在更改框时调用我的 winform 中的函数或事件,并且能够更改 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)   );
        }

和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

        }


    }
    }

知道我可能做错了什么吗?谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慕巷 2024-10-28 20:47:16

MyWPFControl 中公开一个事件,例如:

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));
    }
}

然后在您的表单中订阅它,例如:

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);
}

PS:

我无法测试它,因此可能会出现一些错误,但只是为了给出你的想法...;)

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... ;)

饮湿 2024-10-28 20:47:16

如果没有更多上下文,很难说,但您想要的代码似乎可能是:

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

假设 main 是您要调用 Form1 实例status_change

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文