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

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

问题描述

我有一个 WPF ListBox 我想要

I have a WPF ListBox that I would like to

  • ListBox中启用多选,以及
  • 将 ListBox 数据绑定到我的视图模型.

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

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.

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

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.

推荐答案

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

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天全站免登陆