我正在开发一个 WP7 应用程序,并且有一个带有生成按钮的列表框,所有这些按钮都应该通向特定的地方。我不知道如何知道在运行时按下了哪个按钮。该列表是由对象集合生成的,每个对象都有几个属性。这些属性之一包含一个值,我需要获取该值才能知道将用户发送到哪里。
因此,我想要的过程是用户单击列表框中的某个项目,将生成按钮的对象中的属性值传递给单击处理程序,该处理程序将用户发送到正确的位置。
有什么建议吗?
I'm developing a WP7 app and have a listbox with generated buttons all supposed to lead to somewhere specific. I can't figure out how to know which button was pushed at runtime. The list gets generated from a collection of objects with a couple of attributes in each. One of those attributes contain a value that I need to get to be able to know where to send the user.
So my desired process is that the user clicks on an item in the listbox, passing the value of the attribute in the object the button was generated from, to a click handler which sends the user to the right place.
Any suggestions?
发布评论
评论(2)
我假设您的列表框包含一个 ItemTemplate,它为绑定到您的列表的每个项目构造一个按钮?如果是这种情况,在 Click 事件处理程序中,您需要检查被单击按钮的 DataContext:
顺便说一句,如果您使用它进行导航,ListBox 不会给您带来很好的性能。请参阅以下博客文章了解替代方案:
http://www.scottlogic.co.uk/blog/colin/2011/04/a-fast-loading-windows-phone-7-navigationlist-control/
I presume your ListBox contains a ItemTemplate which constructs a Button for each of the items bound to your list? if this is the case, within your Click event handler you need to inspect the DataContext of the button that was clicked:
As an aside, if you are using this for navigation, a ListBox will not give you very good performance. See the following blog post for an alternative:
http://www.scottlogic.co.uk/blog/colin/2011/04/a-fast-loading-windows-phone-7-navigationlist-control/
检查
OnClick
事件处理程序的sender
属性以进行点击处理。或者,您可能想要处理 ListBox 的 SelectionChanged 事件,然后查询 SelectedItem 的内容。
Check the
sender
property of theOnClick
event handler for the click handling.Alternatively you may want to handle the SelectionChanged event of the ListBox and then query the contents of the SelectedItem.