2011-08-08 124 views
1

我在winform中託管一個WPF用戶控件。我在WPF用戶控件中使用WPF控件擴展器(UserControl1)。當我展開或摺疊擴展器時,我的mainform應該會收到通知。如何實現這個?C#處理winform中的WPF用戶控件事件

我試着用以下選項:

  • 聲明中userconttol1委託和事件,並試圖在MainForm的認購 - 沒有幫助
  • 在MainForm的
  • 使用childchanged事件

WPF usercontrol name - usercontrol1 Mainform Name - Form 1 hosted in main form name - 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(); 
     } 

此解決方案不能幫助我。

回答

0

WPF用戶控件的Winforms互操作 - Winforms中

要求處理WPF事件:WPF用戶控件(的UserControl1)包含擴展器(expander1)和膨脹機包含的winform託管3個單選按鈕 和WPF usercontrl(Form1中) 當單選按鈕在用戶控件的主要形式改變應該得到notifed並基於選擇應該彈出一些控件的MainForm

代碼:在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是在相同usercontrol.xaml.cs

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

現在的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!!!!!!!!"); 
     }  
定義的類