使用SelectionMode = Multiple数据绑定ListBox [英] Databinding a ListBox with SelectionMode = Multiple

查看:365
本文介绍了使用SelectionMode = Multiple数据绑定ListBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF ListBox ,我想要




  • 启用多个在 ListBox 中选择,

  • 将ListBox的数据绑定到我的视图模型。



这两个要求似乎不兼容。我的视图模型有一个 ObservableCollection< T> 属性绑定到此ListBox;我在XAML中设置了一个从属性到 ListBox.SelectedItems 属性的绑定。当我编译时,我收到一条错误,指出 SelectedItems 属性是只读的,无法从XAML设置。



<我是否绑定到错误的控制属性?有没有办法将XAML中的多选ListBox绑定到视图模型集合属性?感谢您的帮助。

解决方案

我最终在SelectionChanged事件处理程序中使用一些代码隐藏来设置视图模型属性。比创建对象包装更简单。


I have a WPF ListBox that I would like to

  • Enable multiple selection in the ListBox, and
  • Databind the ListBox to my view model.

These two requirements appear to be incompatible. My view model has an ObservableCollection<T> property to bind to this ListBox; I set up a binding in XAML from the property to the ListBox.SelectedItems property. When I compiled, I got an error saying that the SelectedItems property was read only and could not be set from XAML.

Am I binding to the wrong control property? Is there a way to bind a multiple-selection ListBox in XAML to a view model collection property? Thanks for your help.

解决方案

I ended up using a bit of code-behind in a SelectionChanged event handler to set the view model property. Simpler than creating object wrappers.

这篇关于使用SelectionMode = Multiple数据绑定ListBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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