如何知道在列表框中按下了哪个生成的按钮?

发布于 2024-11-08 20:43:55 字数 222 浏览 0 评论 0 原文

我正在开发一个 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毁我热情 2024-11-15 20:43:55

我假设您的列表框包含一个 ItemTemplate,它为绑定到您的列表的每个项目构造一个按钮?如果是这种情况,在 Click 事件处理程序中,您需要检查被单击按钮的 DataContext:

private void Button_Click(object sender, RoutedEventArgs e)
{
  Button btn = sender as Button;
  var myObject = btn.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:

private void Button_Click(object sender, RoutedEventArgs e)
{
  Button btn = sender as Button;
  var myObject = btn.DataContext;
}

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/

琉璃繁缕 2024-11-15 20:43:55

检查 OnClick 事件处理程序的 sender 属性以进行点击处理。

或者,您可能想要处理 ListBox 的 SelectionChanged 事件,然后查询 SelectedItem 的内容。

Check the sender property of the OnClick 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文