ListBox ItemTemplate中的按钮未选择项目 [英] Button within ListBox ItemTemplate not selecting item

查看:59
本文介绍了ListBox ItemTemplate中的按钮未选择项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了ListBox控件的情况.项目模板内有一个按钮.当我单击按钮时,我要选择所述ListBox的项目以更改为按钮所在的项目.此刻,我可以通过单击项目模板内的其他位置来更改选定的项目,但是如果我选择按钮.为了澄清评论者的最后一句话,如果单击不是按钮的项目模板,则SelectedItem将按预期更改.如果单击项目模板中的按钮,则SelectedItem不会更改.

I have a situation with a ListBox control. Inside the item template there is a button. When I click the button I want to selected item of said ListBox to change to the item the button is in. At the moment, I can change the selected item by clicking elsewhere within the item template, but that doesn't work if I select the button. To clarify that last sentence for a commenter, if one clicks in the item template that is not the button, the SelectedItem will change as expected. If you click on the button within the item template, the SelectedItem will not change.

更多信息:我正在使用MVVM,并且该按钮已在视图模型中附加了命令.任何解决方案都需要允许它继续工作.

Further information: I am using MVVM and the button has attached to it a command within a view model. Any solution would need to allow this to continue to work.

ListBox链接到ItemSource,ListBox的SelectedItem绑定到视图模型中的属性.

The ListBox is linked to ItemSource, and the SelectedItem of the ListBox is bound to a property in the view model.

如果有固定的方法可以做到这一点,那么到目前为止,我一直找不到它.

If there's a set way to do this, I've so far been unable to find it.

我使用的是 C# 和 Visual Studio 2010.

I am using C# and Visual Studio 2010.

谢谢.

推荐答案

如果可以使用 ToggleButton 并将 IsChecked 绑定到 ListBoxItem IsSelected 属性,就可以了.
之所以未选择您的 ListBoxItem ,是因为该按钮正在处理 MouseDown 事件,从而使 ListBoxItem 不知道该点击.在您的按钮中,为 Click 创建一个事件处理程序,并设置 e.Handled = false; .

If you can use ToggleButton and Bind the IsChecked to the ListBoxItem IsSelected property then it will be fine.
Reason because your ListBoxItem is not selected is because the Button is handling MouseDown event, thus making the ListBoxItem unaware of the click. In your button create an event handler for Click and set e.Handled = false;.

这篇关于ListBox ItemTemplate中的按钮未选择项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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