WPF 如何找到被单击的列表框项目
我有一个 WPF 应用程序,其中有一个列表框,其中填充了“匹配”类型的项目。 如何使按钮(包含在项目中)实际选择该项目以便我可以提取该值?
这是我的代码:两者都不起作用,因为单击按钮实际上并未选择该项目
private void LayButton_Click(object sender, RoutedEventArgs e)
{
var x = (Market)ListBoxSelectedMarket.SelectedItem;
var y = (sender as ListBoxItem);
}
谢谢
I have a WPF application in which there's a listbox filled with items of type 'Match'.
How do I make the button(contained within the item) actually select the item so that I might extract the value?
Here is my code: neither works since clicking the button doesn't actually select the item
private void LayButton_Click(object sender, RoutedEventArgs e)
{
var x = (Market)ListBoxSelectedMarket.SelectedItem;
var y = (sender as ListBoxItem);
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该能够从单击的按钮使用 DataContext 并从那里获取 ListBoxItem 容器,然后选择它。
You should be able to use the DataContext from the clicked Button and get the ListBoxItem container from there, and then select it.
如果您要绑定到一个对象,则可以使用替代方法(在 VB 中),
这将为您提供一个要使用的对象实例,并保存列表框中的任何映射字段
If you are binding to an object an alternative method could be (in VB)
This then gives you an instance of your object to play with and saves you having any mapping fields on the listbox
我没有做过太多 WPF 编程,但如果它与 WinForms 容器对象的工作方式相同,您可以尝试获取按钮的父级。
I haven't done much WPF programming, but you could try getting the parent of the button if it works the same as a WinForms container object.