多选 WPF 列表框只需单击一次即可进行多项选择
我有一个绑定到数据表的数据绑定多选列表框。 当我选择一个列表框项时,我希望自动选择同一列表框中的其他一些列表框项。 我希望通过单击选择多个项目。 我怎样才能做到这一点? 我无法在 SelectionChanged
事件中执行此操作,因为它会导致再次调用同一事件并完全破坏我的逻辑。
请帮忙。 任何帮助将不胜感激。
更新:
我的列表框已绑定到具有 IsSelected 列的数据表。我在样式设置器中使用此列的值来选择列表框项。假设我在数据表中有 10 行。现在,如果用户选择第二个列表框项目,我可以将数据库中相应行的 isselected 设为 1。
但是我如何才能同时选择其他项目呢? 我认为正如肯特所说,我宁愿使用属性进行绑定。 但是我如何使用属性将列表框绑定到数据表?
I have a databound multiselect listbox bound to a datatable. When I select a listboxitem I want some other listboxitems in the same listbox to be selected automatically. I want multiple items to be selected with a single click. How can i do that? I can't do it in SelectionChanged
event because it leads to calling the same event again and breaks my logic altogether.
Please help. Any help will be highly appreciated.
UPDATE:
My listbox is already bound to a datatable which has a IsSelected column.I am using the value of this column in a style setter to make the listboxitem selected.Suppose i have 10 rows in the datatable.Now if the user selects the second listboxitem,i can get the isselected of the correspondong row in the database as 1.
But how can i get the other items to select at the same time? I think as Kent said,I rather use a property for binding. But how can i use a property to bind a listbox to a datatable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将
IsSelected
绑定到数据类中的属性。 当属性更改时,执行逻辑以更新其他数据对象中的IsSelected
属性:然后在您的数据类中,您可以具有如下内容:
或者您可以让数据项引发
IsSelectedChanged
事件并让所属类管理选择的相互依赖性。Bind
IsSelected
to a property in your data class. When the property is changed, execute the logic to update theIsSelected
property in other data objects:Then in your data class you can have something like this:
Or you could have the data item raise an
IsSelectedChanged
event and have the owning class manage the interdependencies of the selection.是否有一个“SelectedItem”属性,其逻辑在该属性的设置器中,可以处理选择其他“喜欢”的项目?
这也许就是我会走的路,如果没有更多细节很难说。
Would having a "SelectedItem" property with the logic in the setter for that property that would handle selecting your other 'like' items?
That's perhaps the way I would go, hard to say with out more details.
我正在做类似的事情。
我有单选组合框,我使用数据库中的“选定值”属性加载它们,现在我正在处理多选列表框,我在数据库中有一个选择列表,需要绑定到所选列表对于我的列表框。
我看不到没有循环的方法。
我看到列表框读/写属性用于获取或设置项目、SelectedItem/Index/Value,或者项目或 SelectedItems 的只读属性。
I am working on a similar thing.
I have single select combo-boxes that I load with the Selected Value property from the database, and now I am working on multi-select list boxes for-which I have a list of selections in the database I need to bind to the selected list for my list box.
I don't see a way to do it without a loop.
I see listbox read/write properties for getting or setting the Items, SelectedItem/Index/Value, or read only properties for Items or SelectedItems.
也许这是作弊,但是,当您在 SelectionChanged 事件中添加项目时,您是否尝试在选择多个项目时将 IsEnabled 设置为 false,然后将其设置回 true 后记,我认为这应该阻止控件事件触发?
Maybe this is cheating, but, when you are adding the items in the SelectionChanged event have you tried setting IsEnabled false while you selected the multiple items and then setting it back to true afterwords, I think that is supposed to keep the controls events from firing?
我创建了一个 MultiSelectCollectionView,您可能会在这里发现有用:
http://grokys.blogspot.com/2010/07/mvvm-and-multiple-selection-part-iii.html
I have created a MultiSelectCollectionView that you might find useful here:
http://grokys.blogspot.com/2010/07/mvvm-and-multiple-selection-part-iii.html