我终于让我的UserControl做我需要的一切,生成一个带有图标和文本的按钮,但是当我尝试将click事件传递到我的主窗口时,我发现它会触发两次!在下面的缩写代码中,我的测试MessageBox响应单击而出现两次。也就是说,它出现了一次。我点击OK,它第二次出现。我第二次单击OK后继续。感谢您查看此内容。
<UserControl x:Class=ImageButton"
....
<Button Style="{DynamicResource ImageButtonStyle}" Click="uc_Click"/>
</UserControl>
----
Public Class ImageButton
Public Event Click as RoutedEventHandler
....
Private Sub uc_Click(sender as Object, e As RoutedEventArgs)
RaiseEvent Click(Me, e)
End Sub
End Class
----
<Window x:Class="MainWindow"
....
<local:ImageButton x:Name=NewItem" Click="NI_Click"></local:ImageButton>
----
Class MainWindow
....
Private Sub NI_Click(sender As Object, e As RoutedEventArgs) Handles NewItem.Click
MessageBox.Show("Success")
End Sub
End Class
答案 0 :(得分:0)
在XAML和代码隐藏中添加两个事件处理程序。
只需从后面的Handles NewItem.Click
类代码中删除MainWindow
:
Private Sub NI_Click(sender As Object, e As RoutedEventArgs)
MessageBox.Show("Success")
End Sub
或从视图中删除Click="NI_Click"
:
...
<local:ImageButton x:Name="NewItem"></local:ImageButton>
...