我有一个带有wpf usercontrol的winform(ElementHost1)。 usercontrol只包含一个按钮。我怎么知道winform中点击了wpf按钮的时间?如何将事件从wpf usercontrol“重定向”到winform?
感谢。
答案 0 :(得分:3)
此 link 可能对您有所帮助。
或者在VB.NET中进行简单的事件处理
Public Event ClickMe()
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
RaiseEvent ClickMe()
End Sub
然后在您的实际窗口中,您可以拥有:
Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler SampleClick1.ClickMe, AddressOf Sample_Click
End Sub
Private Sub Sample_Click()
MessageBox.Show("This is a proof!")
End Sub
SampleClick1
变量来自生成的设计器代码,供您使用。
Friend WithEvents ElementHost1 As System.Windows.Forms.Integration.ElementHost
Friend SampleClick1 As WindowsApplication1.SampleClick
答案 1 :(得分:1)
这是我找到的一个解决方案
在UserControl1.Xaml.cs中
public static RoutedEvent ChkBoxChecked = EventManager.RegisterRoutedEvent("CbChecked", RoutingStrategy.Bubble,
typeof(RoutedEventHandler), typeof(CheckBox));
public event RoutedEventHandler CbChecked
{
add
{
AddHandler(ChkBoxChecked, value);
}
remove
{
RemoveHandler(ChkBoxChecked, value);
}
}
private void cbTreeView_Checked(object sender, RoutedEventArgs e)
{
RoutedEventArgs args = new RoutedEventArgs(ChkBoxChecked);
RaiseEvent(args);
}
现在在MainForm Form1显示的事件中,我们可以添加CbChecked事件
private void Form1_Shown(object sender, EventArgs e)
{
this.elemetHost1.CbChecked += new System.Windows.RoutedEventHandler(wpfusercontrol_CbChecked);
//elementHost1 is the name of wpf usercontrol hosted in Winform
}
void elementHost1_CbChecked(object sender, System.Windows.RoutedEventArgs e)
{
//This event will raise when user clicks on chekbox
}
我在这里面临一个问题。我正在Form1中为UserControl1.so中的所有复选框clickevents发起相同的事件。我想知道在mainform.i中点击了哪个复选框试图在RoutedEventArgs中查看。 ......但没有帮助 如何知道在主窗体中单击了哪个复选框