简体   繁体   English

C#-在Winforms中处理WPF用户控件事件

[英]C# - handling WPF usercontrol events in winforms

I am hosting a WPF usercontrol in winform. 我在Winform中托管WPF用户控件。 I am using WPF control Expander in WPF usercontrol( UserControl1 ). 我在WPF usercontrol( UserControl1 )中使用WPF控件扩展器。 When i expand or collapse the expander my mainform should get notified. 当我展开或折叠扩展器时,我的主窗体应得到通知。 How to achive this? 如何做到这一点?

I tried with the following options: 我尝试了以下选项:

  • Declared a delegate and event in userconttol1 and tried to subscribe in mainform - doesn't help 在userconttol1中声明了一个委托和事件,并试图以mainform进行订阅-无济于事
  • used childchanged event in mainform 主要形式中使用过的childchanged事件

WPF usercontrol name - usercontrol1 Mainform Name - Form 1 hosted usercontrol in main form name - elementHost1 WPF用户控件名称-usercontrol1主窗体名称-主窗体名称中的Form 1托管用户控件-elementHost1

this.elementHost1.ChildChanged += new System.EventHandler<System.Windows.Forms.Integration.ChildChangedEventArgs>(this.elementHost1_ChildChanged);//form1 designer


        private void elementHost1_ChildChanged(object sender, ChildChangedEventArgs e)
        {
            var ctr = (elementHost1.Child as UserControl1);
            if (ctr == null)
                return;
            ctr.isCollapsed+=new UserControl1.expandedDel(ctr_isCollapsed);

        }

    void ctr_isCollapsed(object sender, System.Windows.RoutedEventArgs e)
        {
            throw new NotImplementedException();
        } 

This solution doesn't help me. 此解决方案对我没有帮助。

WPF usercontrol Winforms interop - handling WPF events in Winforms WPF用户控件Winforms互操作-在Winforms中处理WPF事件

Requirement: WPF usercontrol(UserControl1) contains Expander(expander1) and expander contains 3 radio buttons and WPF usercontrl hosted in winform(Form1) when radio button changed in usercontrol main form should get notifed and based on selection it should popup some controls in mainform 要求:WPF usercontrol(UserControl1)包含Expander(expander1),expander包含3个单选按钮,当在usercontrol主表单中更改单选按钮时,应通知Winform(Form1)中托管的WPF usercontrl,并应根据选择在主表单中弹出一些控件

Code:in usercontrol1.xaml.cs 代码:在usercontrol1.xaml.cs中

    public delegate void ucRadioButtonHandler(object sender, **ucButtonEventArgs** e);
    public event ucRadioButtonHandler onRadioButtonClick;

    private void radioButton1_Checked(object sender, RoutedEventArgs e)
    {
        if (onRadioButtonClick != null)
        {
            onRadioButtonClick(sender, new ucButtonEventArgs());
        }
    }

ucButtonEventArgs is a class defined in same usercontrol.xaml.cs ucButtonEventArgs是在同一usercontrol.xaml.cs中定义的类

public partial class ucButtonEventArgs : EventArgs
{
    public ucButtonEventArgs()
    {
    }
}

Now in MainForm Form1 现在处于MainForm Form1中

public Form1()
        {
            InitializeComponent(); 
            userControl11.onRadioButtonClick += new           WpfControlLibrary1.UserControl1.ucRadioButtonHandler(userControl11_onRadioButtonClick);
        }

 void userControl11_onRadioButtonClick(object sender, WpfControlLibrary1.ucButtonEventArgs e)
        {
            System.Windows.Controls.RadioButton rb = (System.Windows.Controls.RadioButton)sender;
            MessageBox.Show(rb.Content + " Selected!!!!!!!!");
        }      

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM