【发布时间】:2011-05-02 07:48:04
【问题描述】:
我有一个带有 wpf 用户控件的 winform (ElementHost1)。用户控件只包含一个按钮。我如何知道何时在我的 winform 中单击了 wpf 按钮?如何将事件从 wpf 用户控件“重定向”到 winform?
谢谢。
【问题讨论】:
标签: wpf vb.net wpf-controls
我有一个带有 wpf 用户控件的 winform (ElementHost1)。用户控件只包含一个按钮。我如何知道何时在我的 winform 中单击了 wpf 按钮?如何将事件从 wpf 用户控件“重定向”到 winform?
谢谢。
【问题讨论】:
标签: wpf vb.net wpf-controls
这个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
【讨论】:
这是我找到的一个解决方案
在 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 中的所有复选框单击事件发出相同的事件。所以我想知道在主窗体中单击了哪个复选框。我试图在 RoutedEventArgs e 中查看。 ...但没有帮助 如何知道主窗体中点击了哪个复选框
【讨论】: