在wpf应用程序中需要动态按钮的帮助 [英] need help in dynamic button in wpf application

查看:49
本文介绍了在wpf应用程序中需要动态按钮的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在按钮点击时创建了一个动态按钮。动态按钮在点击时调用一个窗口





 私有  Sub  Button2_Click( ByVal  sender  As  System。 Object  ByVal  e  As  System.Windows.RoutedEventArgs) Handles  Button2.Click 


Dim button1 As System.Windows.Controls.Button = System.Windows.Controls.Button()


结束 Sub

解决方案

例如类似的东西:

 公开 部分  MyWindow 
继承窗口

公共 Sub ()
InitializeComponent()
结束 Sub ' MyWindow

受保护的 覆盖 Sub OnContentRendered(e As EventArgs)
MyBase .OnContentRendered(e)
Dim myButton As New Button()
myButton.Content = _ M y按钮 ' 或其他东西
' 我们假设您在XAML中创建了一些面板,例如,网格,
' 并将其命名为myGrid;然后添加一个按钮:
myGrid.Children.Add(myButton)
' ...安排等等。
myButton.Click + = Sub (发件人 As 对象,eventArgs As RoutedEventArgs)
AddHandler myButton.Click, Sub (发件人作为 对象,eventArgs 作为 RoutedEventArgs)
' ...从这里调用一些方法,使用sender,eventArgs,但通常不是,
' < span class =code-comment>来处理点击事件
结束 Sub ' myButton.Click handler

结束 Sub ' < span class =code-comment> OnContentRendered

结束 ' class MyWindow





当然,即使窗口已经显示,你也可以在你想要的事件上添加一个按钮。

我演示了向调用列表添加处理程序的最方便的方法路由事件实例,具有匿名委托。您不仅不需要编写您不使用的方法,而且您可以使用方便的签名处理方法,而不会同时传递 sender eventArgs 参数如果你不需要它们。



-SA

i created a dynamic button in button click .how the dynamic button call a window when it is clicked


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As                    System.Windows.RoutedEventArgs) Handles Button2.Click


  Dim button1 As System.Windows.Controls.Button = New System.Windows.Controls.Button()

      
   End Sub

解决方案

For example something like that:

Public Partial Class MyWindow
	Inherits Window

	Public Sub New()
		InitializeComponent()
	End Sub	'MyWindow

	Protected Overrides Sub OnContentRendered(e As EventArgs)
		MyBase.OnContentRendered(e)
		Dim myButton As New Button()
		myButton.Content = "_My Button" ' or something
		' let's assume you created some panel in XAML, for example, a grid,
		' and named it "myGrid"; then add a button:
		myGrid.Children.Add(myButton)
		' ... arrange it, etc.
		myButton.Click += Sub(sender As Object, eventArgs As RoutedEventArgs) 
                AddHandler myButton.Click, Sub(sender As Object, eventArgs As RoutedEventArgs)
                   ' ... call some method(s) from here, using sender, eventArgs, but usually not,
                   ' to handle the click event
                End Sub 'myButton.Click handler

	End Sub	'OnContentRendered

End Class 'class MyWindow



Of course, you can add a button on the event you want, even when the window is already showing.
I demonstrated the most convenient way to add a handler to the invocation list of a routed event instance, with anonymous delegate. Not only you won''t need to write method which you don''t use otherwise, but you can use handling method with the signature convenient for you, without passing both sender and eventArgs parameters if you don''t need them.

—SA


这篇关于在wpf应用程序中需要动态按钮的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆